difficult start with asyncio async/await
lacsaP Patatetom
patatetom at gmail.com
Wed Mar 23 10:54:22 EDT 2022
some redesigns around asyncio.gather gave me what I wanted.
here is the code used :
```python
import asyncio
from random import randint
from termcolor import colored
from datetime import datetime
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, 3))
# 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, 3))
async def runItems(url):
items = await getItems(url)
posts = [ postItem(item, 'localhost', url) for item in items ]
posts and await asyncio.gather(*posts)
async def loopForever():
while True:
print(colored('\n' + datetime.now().strftime('%H:%M:%S') + ' looping...', 'red'))
tasks = [ runItems(url) for url in urls ]
tasks and await asyncio.gather(*tasks)
await asyncio.sleep(30)
asyncio.run(loopForever())
```
do you think this is the right way for what I want to do ?
regards, lacsaP.
Le mercredi 23 mars 2022 à 14:15:08 UTC+1, lacsaP Patatetom a écrit :
> 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