Answer the question
In order to leave comments, you need to log in
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()
1
2
1
2
1
2
1
2
1
1
2
2
1
1
2
2
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question