Python Glossary Русский

Python 3.4+multiprocessingterm

multiprocessing.SimpleQueue

communication

A simplified inter-process queue: only put(), get(), empty() and close(), with no background thread, timeouts or maxsize.

Syntax

q = ctx.SimpleQueue(); q.put(obj); q.get(); q.empty(); q.close()

With no background thread put() writes to the pipe itself: a pickle error is raised right from put() instead of getting lost on stderr, but with a full pipe put() blocks until a reader drains it. get() has no timeout, so it waits forever if nobody writes. It is not queue.SimpleQueue from the queue module: that one is for threads and does not work across processes. close() appeared in 3.9.

Examples

import multiprocessing as mp
def worker(q):
    q.put('ready')
if __name__ == '__main__':
    ctx = mp.get_context('spawn')
    q = ctx.SimpleQueue()
    p = ctx.Process(target=worker, args=(q,))
    p.start()
    print(q.get())  # → ready
    p.join()
    q.close()
import multiprocessing as mp
if __name__ == '__main__':
    q = mp.get_context('spawn').SimpleQueue()
    try:
        q.put(lambda: 1)
    except Exception as e:
        print('raised at put:', type(e).__name__)  # → raised at put: PicklingError
    print(q.empty())  # → True
    q.close()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy