A
A
aquario-cloud2021-11-07 00:03:40
Python
aquario-cloud, 2021-11-07 00:03:40

Why does the same code give different answers?

import aiohttp
import asyncio
from scripts import config

URL = f'http://xmlriver.com/search/xml?user={config.USERID}&key={config.USERKEY}&groupby=100&country=2376&lr=IW&domain=65&device=desktop&query='

keywords = ['start', 'stop', 'test', 'experimental']
urls = [
    'http://xmlriver.com/search/xml?user={}&key={}&groupby=100&country=2376&lr=IW&domain=65&device=desktop&query={}'.format(
        config.USERID, config.USERKEY, keyword
    ) for keyword in keywords
]

async def boundFetch(semaphore, url):
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                print('1')
                await asyncio.sleep(0.1)
                await response.text()
                print('2')

async def main():
    limit = asyncio.BoundedSemaphore(value=config.LIMIT)
    tasks = [boundFetch(limit, url) for url in urls]
    await asyncio.wait(tasks)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

here is the actual code, it gives
1
2
1
2
1
2
1
2

and sometimes
1
1
2
2
1
1
2
2

the question is why the same code produces a different output? how can this even happen?
and if, in asyncio.wait, for example, put 2 seconds, then everything always works as it should

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-07
@aquario-cloud

You are trying to make network requests. The time of their implementation depends on many factors, incl. from the load of communication channels between you and the server, from the caching policy, etc. So, depending on which answer comes first, one or another coroutine can get control.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question