Message 339847 - Python tracker (original) (raw)
The docs for https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Queue.close read:
Indicate that no more data will be put on this queue by the current process. The background thread will quit once it has flushed all buffered data to the pipe. This is called automatically when the queue is garbage collected.
From this text it seems to me as though the queue should be used as follows:
import contextlib import multiprocessing
def worker(q): with contextlib.closing(q): q.put_nowait('hello')
def controller(): q = multiprocessing.Queue() q.close() # no more 'put's from this process p = multiprocessing.Process(target=worker, args=(q, )) p.start() assert q.get() == 'hello' p.join() assert p.exitcode == 0 print('OK!')
if name == 'main': controller()
however I get this:
Traceback (most recent call last): File "controller.py", line 22, in controller() File "controller.py", line 15, in controller assert q.get() == 'hello' File "/usr/lib/python3.7/multiprocessing/queues.py", line 94, in get res = self._recv_bytes() File "/usr/lib/python3.7/multiprocessing/connection.py", line 212, in recv_bytes self._check_closed() File "/usr/lib/python3.7/multiprocessing/connection.py", line 136, in _check_closed raise OSError("handle is closed") OSError: handle is closed