(host_port_queue, protocol_factory, connection_count=4)
return event_q
address_queue: a queue of (host, port) tuples
    protocol_factory: the callback passed to EventLoop.create_connection
    connection_count: number of connections to keep established
address_queue: a queue of (host, port) tuples
    protocol_factory: the callback passed to EventLoop.create_connection
    connection_count: number of connections to keep established
address_queue : a queue of ( host port ) tuples protocol_factory : the callback passed to EventLoop . create_connection connection_count : number of connections to keep established
def manage_connection_count(host_port_queue, protocol_factory, connection_count=4):
    """
    address_queue: a queue of (host, port) tuples
    protocol_factory: the callback passed to EventLoop.create_connection
    connection_count: number of connections to keep established
    """
    event_q = asyncio.Queue()

    # asyncio.Task doesn't create any non-weak references to the task,
    # so we have to put them in a container with a strong reference
    # or they may be garbage collected.
    event_q.tasks = set()

    @asyncio.coroutine
    def run():
        while True:
            host, port = yield from host_port_queue.get()
            logging.debug("got %s:%d from connection pool", host, port)
            logging.info("connecting to %s:%d" % (host, port))
            try:
                transport, protocol = yield from asyncio.get_event_loop().create_connection(
                    protocol_factory, host=host, port=port)
                logging.info("connected (tcp) to %s:%d", host, port)
                event_q.put_nowait(("connect", (host, port), protocol))
                yield from asyncio.wait_for(protocol.connection_lost_future, timeout=None)
                event_q.put_nowait(("disconnect", (host, port), protocol))
            except Exception:
                logging.exception("failed to connect to %s:%d", host, port)

    for i in range(connection_count):
        # we add the tasks to a set on event_q so they're not garbage
        # collected until event_q is.
        event_q.tasks.add(asyncio.Task(run()))

    return event_q
def manage_connection_count ( host_port_queue , protocol_factory , connection_count = 4 ) : event_q = asyncio . Queue ( ) # asyncio.Task doesn't create any non-weak references to the task, # so we have to put them in a container with a strong reference # or they may be garbage collected. event_q . tasks = set ( ) @ asyncio . coroutine def run ( ) : while True : host , port = yield from host_port_queue . get ( ) logging . debug ( "got %s:%d from connection pool" , host , port ) logging . info ( "connecting to %s:%d" % ( host , port ) ) try : transport , protocol = yield from asyncio . get_event_loop ( ) . create_connection ( protocol_factory , host = host , port = port ) logging . info ( "connected (tcp) to %s:%d" , host , port ) event_q . put_nowait ( ( "connect" , ( host , port ) , protocol ) ) yield from asyncio . wait_for ( protocol . connection_lost_future , timeout = None ) event_q . put_nowait ( ( "disconnect" , ( host , port ) , protocol ) ) except Exception : logging . exception ( "failed to connect to %s:%d" , host , port ) for i in range ( connection_count ) : # we add the tasks to a set on event_q so they're not garbage # collected until event_q is. event_q . tasks . add ( asyncio . Task ( run ( ) ) ) return event_q