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"
我不知道为什么它没有结束,也没有打印“全部完成”,只是继续循环或继续执行。 如果您能让这段代码更高效,将会有很大的帮助,但首先我想知道为什么它没有结束。
最佳答案
问题是:
while out.qsize != 0 :
print out.get()
out.qsize
是一个函数,所以现在您将函数本身(而不是返回值!)与 0
进行比较,当然总是 错误
。
您应该使用:
while out.qsize() != 0 :
print out.get()
关于Python多处理,代码继续执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934792/