I wanted to implement a simple python program using parallel execution. This is an I / O limitation, so I decided that threads would match (as opposed to processes). After reading the documentation for the queue and the plug, I thought something like the following might work.
q = Queue.Queue() if os.fork(): # child while True: print q.get() else: # parent [q.put(x) for x in range(10)]
However, the call to get () never returns. I thought it would return when another thread made a put () call. Using the streaming module, everything looks as I expected:
q = Queue.Queue() def consume(q): while True: print q.get() worker = threading.Thread (target=consume, args=(q,)) worker.start() [q.put(x) for x in range(10)]
I just don't understand why the fork approach does not do the same. What am I missing?
source share