difficult start with asyncio async/await

lacsaP Patatetom patatetom at gmail.com
Wed Mar 23 09:14:54 EDT 2022


hi,

difficult start with asyncio async/await...

I'm trying to make a mockup that queries a few sites and posts the results to a server, but the result is not what I expected.

I was expecting to get the 4 "get" one after the other, followed by the "post" (eventually mixed) but I get something that looks more like procedural than anything else.

some help/clarification would be welcome.

here is my code :
```python
import asyncio
from itertools import cycle
from random import randint
from termcolor import colored

urls = ('yellow', 'cyan', 'green', 'magenta')

async def getItems(url):
	print(colored(f'get new items from url {url} with aiohttp...', url))
	# simulate aiohttp request/response
	await asyncio.sleep(randint(1, 5))
	# return a list of some items
	return [ url + str(i) for i in range(1, randint(2, 5)) ]

async def postItem(item, server, url):
    # url is here only to color print
	print(colored(f'post new item {item} to server {server} with aiohttp...', url))
	# simulate aiohttp request/response
	await asyncio.sleep(randint(1, 5))

async def runItems(url):
	items = await getItems(url)
	for item in items:
		await postItem(str(item), 'localhost', url)

async def loopForever():
	while True:
		print(colored('looping...', 'red'))
		for url in urls:
			await runItems(url)
		# sleeping 30s before next loop
		await asyncio.sleep(10)

loop = asyncio.get_event_loop()
tasks = [ loop.create_task(loopForever()) ]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
```

and here is the results:
```
looping...
get new items from url yellow with aiohttp...
post new item yellow1 to server localhost with aiohttp...
post new item yellow2 to server localhost with aiohttp...
post new item yellow3 to server localhost with aiohttp...
post new item yellow4 to server localhost with aiohttp...
get new items from url cyan with aiohttp...
post new item cyan1 to server localhost with aiohttp...
post new item cyan2 to server localhost with aiohttp...
post new item cyan3 to server localhost with aiohttp...
post new item cyan4 to server localhost with aiohttp...
get new items from url green with aiohttp...
post new item green1 to server localhost with aiohttp...
post new item green2 to server localhost with aiohttp...
post new item green3 to server localhost with aiohttp...
post new item green4 to server localhost with aiohttp...
get new items from url magenta with aiohttp...
post new item magenta1 to server localhost with aiohttp...
post new item magenta2 to server localhost with aiohttp...
post new item magenta3 to server localhost with aiohttp...
post new item magenta4 to server localhost with aiohttp...
looping...
get new items from url yellow with aiohttp...
post new item yellow1 to server localhost with aiohttp...
post new item yellow2 to server localhost with aiohttp...
post new item yellow3 to server localhost with aiohttp...
...
```

here is what was expected :
```
looping...
get new items from url yellow with aiohttp...
get new items from url cyan with aiohttp...
get new items from url green with aiohttp...
get new items from url magenta with aiohttp...
post new item...
looping...
get new items from url yellow with aiohttp...
get new items from url cyan with aiohttp...
get new items from url green with aiohttp...
get new items from url magenta with aiohttp...
post new item...
...
```

regards, lacsaP.


More information about the Python-list mailing list