我编写了一个 python 脚本,将市场数据导入到 MariaDB 数据库中。为了加快导入速度,我决定使用模块线程。因此,首先一个函数使用 URL 填充队列,从中下载数据并将其导入到我的数据库中。不幸的是,导入函数似乎只由一个线程而不是多个线程处理。一开始的输出状态:Thread-1Thread-2Thread-3Thread-4这表明创建了 4 个单独的工作人员,但进入 while 循环使看起来只有一个工作人员实际处理获取的 url。Thread-1 processes https://someurl=2 queue# 32Thread-1 processes https://someurl=3 queue# 31Thread-1 processes https://someurl=4 queue# 30Thread-1 processes https://someurl=5 queue# 29Thread-1 processes https://someurl=6 queue# 28Thread-1 processes https://someurl=7 queue# 27Thread-1 processes https://someurl=8 queue# 26Thread-1 processes https://someurl=9 queue# 25Thread-1 processes https://someurl=10 queue# 24Thread-1 processes https://someurl=11 queue# 23Thread-1 processes https://someurl=12 queue# 22Thread-1 processes https://someurl=13 queue# 21Thread-1 processes https://someurl=14 queue# 20Thread-1 processes https://someurl=15 queue# 19Thread-1 processes https://someurl=16 queue# 18Thread-1 processes https://someurl=17 queue# 17Thread-1 processes https://someurl=18 queue# 16我希望输出看起来像(理想情况下):Thread-1 processes https://someurl=2 queue# 32Thread-2 processes https://someurl=3 queue# 31Thread-3 processes https://someurl=4 queue# 30Thread-4 processes https://someurl=5 queue# 29我在这里错过了什么?
1 回答
RISEBY
TA贡献1856条经验 获得超5个赞
为每个工人打印不同的“名字”:
def import_mo(i, urls):
station_id = 60003760
print('Worker', i)
# etc
# later:
print("Worker %s processes %s queue# %s" % (i, url, urls.qsize()))
并创建线程:
for i in range(num_threads):
worker = Thread(target=import_mo, args=(i,urls,))
worker.setDaemon(True)
threads.append(worker)
worker.start()
添加回答
举报
0/150
提交
取消