import threading
import time
import pymysql
from sqlalchemy import create_engine
import pandas as pd
import get_aid_from_docid
dbConnectStr = "mysql+pymysql://user:passward@host:port/database?charset=utf8"
conn = create_engine(dbConnectStr)
client = pymysql.connect(
host=host,
port=int(3306),
user=user,
password=passward,
db=database,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
cursor03 = client.cursor()
lock = threading.Lock()
def getData(start_id,end_id):
sql = f'''select * from adidas_weixin_aid_2_pice1
where key_id between {start_id} and {end_id};'''
print(sql)
df = pd.read_sql(sql,conn)
df.to_excel(f'muti_thread_practice_{threading.current_thread().name}.xlsx')
print(threading.current_thread().name,'数据是',df)
if __name__ == '__main__':
df = pd.read_excel('医生账号库第二批次-小红书AID获取.xlsx')
# sql = f'''select docid from tmp_api021_data;'''
# print(sql)
# df = pd.read_sql(sql,conn)
start = time.time()
t1 = threading.Thread(name = 't1',target = get_aid_from_docid.getAid, args = (df.loc[0:5000],))
t2 = threading.Thread(name = 't2',target = get_aid_from_docid.getAid, args = (df.loc[5001:10000,],))
t3 = threading.Thread(name = 't3',target = get_aid_from_docid.getAid, args = (df.loc[10001:15000,],))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
end = time.time()
print('共用时:',end - start ,'秒')
python多线程访问数据库
最新推荐文章于 2022-11-13 17:00:00 发布