1

I have a server and client where multiple clients will be connecting to multiple server and does something. I want to make client connection to server async.

clients = [1,2,3]
for c in clients:
  connect_to_server()

right now the above code connects to server but waits for first iteration to be executed and then second. how to make it async function call to connect_to_server() method so that second iteration will not wait for first to be executed ? And which function has to be async either connect_to_server or for loop function and which has to be awaiting ?


def connect_to_server(client_id):
    print(client_id)
    time.sleep(3)


async def main():
    clients = [1, 2, 3, 4]
    for client in clients:
       await connect_to_server(client)

asyncio.run(main())

2 Answers 2

1

Here wrap the code you want to be async in an async function,

import asyncio

async def main():

    clients = [1,2,3]

    for c in clients:
        await connect_to_server()

asyncio.run(main())

Explaination : here asyncio calls your async "MAIN" function and "AWAIT" in the function makes the process proceed to the next iteration without waiting for connect_to_server() function to finish !

Sign up to request clarification or add additional context in comments.

4 Comments

I tried the above and I get this error TypeError: object NoneType can't be used in 'await' expression
@VimalanE what does the connect_to_server function does ? Is it even defined or are you running this snippet as it is ?
I have updated the code for you in my question have a look
just for learning am printing the argument in connect_to_server which is not async function
0

Your code will run at least for as much time is necessary to run the syncronous code. The following code illustrates this:

import asyncio
import time

async def connect_to_server(client_id):
    print(client_id)
    print(f'sleep for client_id {client_id}')
    time.sleep(1)
    print(f'async sleep for client_id {client_id}')
    await asyncio.sleep(1)
    print(f'done for client_id {client_id}')

async def main():
    clients = range(10)
    
    # Launch all of the coroutines in parallel
    coroutines = [connect_to_server(client) for client in clients]
    
    # Wait for all corountines to complete
    await asyncio.gather(*coroutines)

start = time.time()
asyncio.run(main())
print(f'Time elapsed: {time.time() - start} seconds')

The code above takes no less than 11 seconds to complete.

Beware! The following code calls await connect_to_server in a loop:

import asyncio
import time

async def connect_to_server(client_id):
    print(client_id)
    print(f'sleep for client_id {client_id}')
    time.sleep(1)
    print(f'async sleep for client_id {client_id}')
    await asyncio.sleep(1)
    print(f'done for client_id {client_id}')

async def main():
    clients = range(10)
    
    # Launch all of the coroutines in parallel
    for client in clients:
        await connect_to_server(client)
    
start = time.time()
asyncio.run(main())
print(f'Time elapsed: {time.time() - start} seconds')

The above code takes no less than 20 seconds to complete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.