简体   繁体   中英

Sharing queue between threads running in different modules

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM