def producer_consumer(producer, consumer, addr='tcp://127.0.0.1',
                      port=None, context=None):
    context_created = False
    if context is None:
        context_created = True
        context = zmq.Context()
    try:
        consumer_socket = context.socket(zmq.PULL)
        if port is None:
            port = consumer_socket.bind_to_random_port(addr)
        try:
            process = _spawn_producer(producer, port)
            result = consumer(consumer_socket)
        finally:
            process.terminate()
        return result
    finally:
        # Works around a Python 3.x bug.
        if context_created:
            context.destroy()
A producer-consumer pattern.
dataset/ETHPy150Open mila-udem/fuel/fuel/utils/parallel.py/producer_consumer