Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 877
Yan Yi Avatar asked Apr 18 '26 18:04

Yan Yi


1 Answers

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
like image 104
Daniel Roseman Avatar answered Apr 20 '26 12:04

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!