簡體   English   中英

Python多處理和隊列

[英]Python Multiprocessing and Queues

我有一個摘自Python Workers and Queues的另一個stackoverflow的代碼片段

from multiprocessing import Process
from Queue import Queue

class Worker(Process):
    def __init__(self, queue):
        super(Worker, self).__init__()
        self.queue= queue

    def run(self):
        print 'Worker started'
        # do some initialization here

        print 'Computing things!'
        for data in iter( self.queue.get, None ):
            print(data)

if __name__ == '__main__':       
    request_queue = Queue()
    for i in range(4):
        Worker( request_queue ).start()
    for data in range(100):
        request_queue.put( data )
    # Sentinel objects to allow clean shutdown: 1 per worker.
    for i in range(4):
        request_queue.put( None ) 

為什么此過程掛起而不處理隊列內容?

發現我的錯誤。 我不知道有兩個隊列。

改變

 from multiprocessing import Process
 from Queue import Queue

 from multiprocessing import Process,Queue

現在我有預期的行為

暫無
暫無

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

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