问题
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