2
from multiprocessing import Process , Queue
from datetime import datetime
c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
out = Queue()
def support(m):
for k in m :
print "%s <-- hi" % k
out.put("done")
all = Queue()
temp = []
total = len(c)
count = 0
for m in c :
count += 1
total = total - 1
temp.append(m)
if count == 5 or total == 0 :
all.put(temp)
count = 0
temp = []
process_count = 3
while all.qsize() != 0 :
process_list = []
try :
for x in range(process_count) :
p = Process(target=support, args=(all.get(),))
process_list.append(p)
for p in process_list :
p.start()
for p in process_list :
p.join()
except Exception as e :
print e
while out.qsize != 0 :
print out.get()
print "all done"
我不知道它爲什麼不結束,不打印「全部完成」,只是保持不斷循環或繼續執行。 如果你能使這段代碼更有效率,但首先我想知道爲什麼它不會結束會有很大的幫助。Python多處理,代碼繼續執行?
無關:'queue.qsize()'返回隊列* *近似大小。你可以調用'queue.get_nowait()'來獲取'Empty'異常。 – jfs