python multiprocess.Queue - RuntimeError: Queue objects should only be shared between processes through inheritance


相關代碼如下:

queue = multiprocessing.Queue()

db = HandleSQL(conf_db, data_db, queue)

錯誤信息如下:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.6/threading.py", line 484, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in _handle_tasks
    put(task)
  File "/usr/lib/python2.6/multiprocessing/queues.py", line 51, in __getstate__
    assert_spawning(self)
  File "/usr/lib/python2.6/multiprocessing/forking.py", line 25, in assert_spawning
    ' through inheritance' % type(self).__name__
RuntimeError: Queue objects should only be shared between processes through inheritance

 

修改如下:

manager = multiprocessing.Manager()
queue = manager.Queue()

 

Queue對象只能使用繼承(inheritance)的方式共享。這是因為Queue本身基於unix的Pipe對象實現,而Pipe對象的共享需要通過繼承。

因此,在一個典型的應用實現模型當中,應該是父進程創建Queue,然后創建子進程共享該Queue,由父進程和子進程分別讀寫。另一種實現方式是父進程創建Queue,創建多個子進程,有的子進程讀Queue,有的子進程寫Queue。

詳見參考中的鏈接。

 

參考:

http://stackoverflow.com/questions/3217002/how-do-you-pass-a-queue-reference-to-a-function-managed-by-pool-map-async

http://blog.ftofficer.com/2009/11/using-python-multiprocessing-1-process-model/

http://blog.ftofficer.com/2009/12/python-multiprocessing-2-object-sharing-across-process/

http://blog.ftofficer.com/2009/12/python-multiprocessing-3-about-queue/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM