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"
我不知道为什么它没有结束并且不打印“全部完成”,只是保持连续循环或继续执行。 如果你可以使这个代码更有效率,但首先我想知道为什么它不会结束,将会有很大的帮助。
答案 0 :(得分:3)
问题是:
while out.qsize != 0 :
print out.get()
out.qsize
是一个函数,所以现在你将函数本身(不是返回值!)与0
进行比较,当然总是False
。
您应该使用:
while out.qsize() != 0 :
print out.get()