I'd like to use a shared queue from multiple threads and modules. I have the following Python code:
# moda.py
import queue
import modb
q = queue.Queue()
def myPut(x):
q.put(x)
def main():
print('moda:', str(id(q)))
modb.go()
q.get()
if __name__ == '__main__':
main()
and
# modb.py
import moda
import threading
def something():
print('modb:', str(id(moda.q)))
moda.myPut('hi')
def go():
threading.Thread(target = something).start()
something
gets called on thread 1, somethingElse
gets called on thread 2. The addresses of q
are different in these two methods - which is why the call to get
never returns. How can I avoid this? Is it because of the cyclic import or because of multithreading?