Python多处理,代码继续执行?

标签 python queue multiprocessing

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/

相关文章:

Python多处理: Kill worker on exit

python - 将接受类成员函数作为变量的函数传递给python multiprocess pool.map()

python - 将多个函数应用于 numpy 数组的每一行

python - Fillna 如果出现频率最高,则 fillna 为整个列中出现频率最高的值

python - Web.py db.update() - 将字典作为参数传递,其中 AND 作为值列表

yii2 - 如何使用 Yii2 Queue::EVENT_AFTER_ERROR 事件

python - 如何从python中的列中整理出重复的条目

c# - 如何将多个异步方法设置到队列中?

queue - JMS 是否依赖于平台? (例如 Java<->.Net)

python - python多处理守护进程中的僵尸进程