Re: Get multiprocessing.Queue to do priorities
- From: uuid <M8R-gqmgb4@xxxxxxxxxxxxxx>
- Date: Sun, 10 May 2009 00:11:21 +0200
The Queue module, apparently, is thread safe, but *not* process safe. If you try to use an ordinary Queue, it appears inaccessible to the worker process. (Which, after all, is quite logical, since methods for moving items between the threads of the same process are quite different from inter-process communication.) It appears that creating a manager that holds a shared queue might be an option (http://stackoverflow.com/questions/342556/python-2-6-multiprocessing-queue-compatible-with-threads).
Just
for illustration: This shows that Queue.Queue doesn't work with processes:
------------------------
def worker(queue):
while True:
item = queue.get()
print item
queue.task_done()
queue_queue = Queue.Queue()
worker_thread = multiprocessing.Process(target=worker, args=(queue_queue,))
worker_thread.start()
for i in range(10):
queue_queue.put(str(i))
time.sleep(10)
while True:
try:
print 'still on queue: ' + queue_queue.get(False)
except Queue.Empty:
break
worker_thread.join()
------------------------
This yields:
still on queue: 0
still on queue: 1
still on queue: 2
still on queue: 3
still on queue: 4
still on queue: 5
still on queue: 6
still on queue: 7
still on queue: 8
still on queue: 9
So no queue item ever arrives at the worker process.
On 2009-05-09 22:00:36 +0200, Scott David Daniels <Scott.Daniels@xxxxxxx> said:
2.6 has a PriorityQueue in the Queue module.
If you aren't using 2.6, you could copy the code for your own version.
.
- Follow-Ups:
- Re: Get multiprocessing.Queue to do priorities
- From: Jesse Noller
- Re: Get multiprocessing.Queue to do priorities
- References:
- Get multiprocessing.Queue to do priorities
- From: uuid
- Re: Get multiprocessing.Queue to do priorities
- From: uuid
- Re: Get multiprocessing.Queue to do priorities
- From: Scott David Daniels
- Re: Get multiprocessing.Queue to do priorities
- From: uuid
- Re: Get multiprocessing.Queue to do priorities
- From: Scott David Daniels
- Get multiprocessing.Queue to do priorities
- Prev by Date: Re: Why there is a parameter named "self" for classmethod function?
- Next by Date: Re: Wrapping comments
- Previous by thread: Re: Get multiprocessing.Queue to do priorities
- Next by thread: Re: Get multiprocessing.Queue to do priorities
- Index(es):
Relevant Pages
|