簡體   English   中英

Python多處理,代碼繼續執行嗎?

[英]Python multiprocessing , code keep on executing?

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"

我不知道為什么它沒有結束並且不打印“ all done”,只是連續不斷地循環或繼續執行。 如果您可以使此代碼更有效,將有很大幫助,但首先,我想知道為什么它沒有結束。

問題是:

while out.qsize != 0 :
    print out.get()

out.qsize是一個函數,因此現在您將函數本身(而不是返回值!)與0 ,with當然總是False

您應該使用:

while out.qsize() != 0 :
    print out.get()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM