Sharing queue between threads running in different modules

旧城冷巷雨未停 提交于 2021-01-28 02:19:29

问题


I have some modules in different packages for my project. This project requires several threads which might be started in different modules, and I intend to use queues for the inter-thread communication.

Is there a way to pass a queue created in one module for use in another module?

# ModuleA.py    

myQueue = Queue.Queue()
thread = myThread(threadID1, tName1, myQueue)
thread2 = myThread(threadID2, tName2, myQueue)

# ModuleB.py

myQueue = get_the_previous_queue_created() # possible?
thread3 = myThread(threadID3, tName3, myQueue)

回答1:


A Queue is not different from any other object, in that if it is defined at module level in one module you can import that module and access it directly:

import ModuleA
# now ModuleA.myQueue is the Queue object created there


来源:https://stackoverflow.com/questions/32586233/sharing-queue-between-threads-running-in-different-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!