Home > Blockchain >  Asynchronous requests inside the for loop in python
Asynchronous requests inside the for loop in python

Time:04-15

I have this snippet

config = {10: 'https://www.youtube.com/', 5: 'https://www.youtube.com/', 7: 'https://www.youtube.com/',
      3: 'https://sportal.com/', 11: 'https://sportal.com/'}

def test(arg):

    for key in arg.keys():
        requests.get(arg[key], timeout=key)


test(config)

On that way the things are happaning synchronously. I want to do it аsynchronously. I want to iterate through the loop without waiting for response for each address and to go ahead to the next one. And so until I iterate though all addresses in dictionary. Than I want to wait until I get all responses for all addresses and after that to get out of test function. I know that I can do it with threading but I read that with asyncio lyb it can be done better, but I couldn't implement it. If anyone have even better suggestions I am open for them. Here is my try:

async def test(arg):

loop = asyncio.get_event_loop()
tasks = [loop.run_in_executor(requests.get(arg[key], timeout=key) for key in arg.keys())]
await asyncio.gather(*tasks)

asyncio.run(test(config))

CodePudding user response:

Here is the solution:

def addresses(adr, to):
    requests.get(adr, timeout=to)

async def test(arg):

    loop = asyncio.get_event_loop()
    tasks = [loop.run_in_executor(None, addresses, arg[key], key) for key    in arg.keys()]
    await asyncio.gather(*tasks)



asyncio.run(test(config))

Now it works аsynchronously with lyb asyncio not with threading.

  • Related