Making connections with python3 asyncio

I am trying to connect to multiple servers at the same time. I am currently using loop.create_connection

, but it hangs on the first non-responsive server.

gsock = loop.create_connection(lambda: opensock(sid), server, port)
transport, protocol = loop.run_until_complete(gsock)

      

I tried this thread but created problems with the sid value as well as various errors like RuntimeError: Event loop is running

and RuntimeError: Event loop stopped before Future completed

. Also, according to my variables (which were mixed), the method is connection_made()

triggered when it transport, protocol = loop.run_until_complete(gsock)

throws an exception.

I am not very good with the async module, so please be as thorough as possible. I don't think I need read / write variables since the read should be done automatically and run the method data_received()

.

Thank.

+3


source to share


1 answer


You can connect to many servers at the same time, scheduling all coroutines at the same time, rather than using loop.run_until_complete

to make each connection individually. One way to do this is to use asyncio.gather

to schedule them all and wait for each one to complete:

import asyncio

# define opensock somewhere

@asyncio.coroutine
def connect_serv(server, port):
    try:
        transport, protocol = yield from loop.create_connection(lambda: opensock(sid), server, port)
    except Exception:
        print("Connection to {}:{} failed".format(server, port))

loop = asyncio.get_event_loop()
loop.run_until_complete(
    asyncio.gather(
      connect_serv('1.2.3.4', 3333),
      connect_serv('2.3.4.5', 5555),
      connect_serv('google.com', 80),
 ))
loop.run_forever()

      



This will cause all three coroutines listed in the call to be gather

at the same time, so if one of them hangs, the others will not be affected; they will be able to continue their work while the other link hangs. Then, if they are all completed, it is executed loop.run_forever()

, which will allow you to continue running until you stop the loop or close the program.

The reader

/ writer

variables listed by you will only matter if you used asyncio.open_connection

to connect to servers than create_connection

. It uses the Stream API, which is a higher level API than the protocol / transport API that is being used create_connection

. It really is up to you to decide which one you prefer to use. There are examples as in the asyncio

docs if you want to see a comparison.

+3


source







All Articles