我怀疑这是 Python for 的一个 bug
資深大佬 : smallgoogle 21
# -*- coding: utf-8 -*- from multiprocessing import Pool import os, time, random def worker(msg, ip): t_start = time.time() print("%s 开始执行, ip:%s, 进程号为%d" % (msg['id'], ip['ip'], os.getpid())) time.sleep(random.random() * 2) # time.sleep(2) t_stop = time.time() print(msg['id'], "执行完毕, ip:%s, 耗时%0.2f" % (ip['ip'], t_stop - t_start)) def start(d): po = Pool(10) a_list = [ {"ip": "ip - 1"}, {"ip": "ip - 2"} ] d_list = d for_times = 0 while len(d_list) > 0: print("第 %s 次" % (for_times)) x = 0 for i in d_list: if x < 2: po.apply_async(worker, (i, a_list[for_times])) d_list.pop(x) x+=1 else: print("==============") break for_times += 1 print("----start----") po.close() po.join() print("----end----") if __name__ == '__main__': d = [{ "id": 1, "keyword": "site:www.aaa.com 灯", "site": "www.aaa.com" }, { "id": 2, "keyword": "site:www.bbb.com 灯", "site": "www.bbb.com" }, { "id": 3, "keyword": "site:www.ccc.com 灯", "site": "www.ccc.com" }, { "id": 4, "keyword": "site:www.ddd.com 灯", "site": "www.ddd.com" }] start(d)
这段代码没问题。可是会报错;我 debug 了。就是 for 里面的 if 的问题。
理论上 whlie 两次就结束了。可是为啥这里 whlie 走了三次呢?
while 第二次的时候,会看到 for 里面的 if 没生效了。
这是不是 python 的 bug ?
我已经排两天。无结果。
大佬有話說 (16)