@
scriptB0y 我用 concurrent 模块重新修改了下代码,发现效率比我之前的代码差了好多....
for 循环: #获取 cookie:
threads = [ (i.get('hotelId'),headersCookie) for i in id_lines.find() ]
pool = ThreadPoolExecutor()
future_tasks = [ pool.submit(start_claw, t) for t in threads ]
wait(future_tasks, return_when=ALL_COMPLETED)
time.sleep(3)
3K 左右的链接,用时 382 秒
for 循环: #获取 cookie:
threads = []
for i in id_lines.find():
hotelId = i.get('hotelId')
threads.append(hotelId)
for hotelid in threads:
t = ClawData(hotelid,headersCookie)
t.setDaemon(True) #防止程序异常退出时,有僵尸进程存在
t.start()
for hotelid in threads:
t.join()
time.sleep(3)
用时:52 秒
请问为啥效率可以差别这么大?