How to make an asynchronous principle of task execution from a task generator?

31 views Asked by At

I have a problem, do you have any ideas for a solution? You need to add asynchronous tasks to the event loop without waiting for them to be completed (using await is not an option, otherwise there is no point in such a program). Just expect new tasks and launch new ones. For example, the code would be clearer. He's not a worker.

import asyncio, random 

async def wrapper(word: str):
    print(word)
    await asyncio.sleep(1) # task simulation
 
def generator():
    abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    while True:
        yield random.choice(abc)
        
async def manager():
    loop = asyncio.get_event_loop()
    for letter in generator():
        loop.create_task(wrapper(letter)) # here is something like performing tasks asynchronously
        
    
asyncio.run(manager())

I tried to create a longpull server from which I receive events from the user. I wanted to process certain events asynchronously. But so far it works only synchronously.

1

There are 1 answers

0
jsbueno On

async def manager():
    loop = asyncio.get_event_loop()
    for letter in generator():
        loop.create_task(wrapper(letter)) # here is something like performing tasks asynchronously

After you call create_task, just keep a reference around to the task created, and perform some awaits inside that "for" in your manager, so that the other tasks have an opportunity to run: the main difference between coding for async and coding for multi-threaded code, is that concurrent code is never exectuted if you don't yield to the main loop.

These changes in your manager are enough:

async def manager():
    with asyncio.TaskGroup() as tg():
        for letter in generator():
            tg.create_task(wrapper(letter)) # here is something like performing tasks asynchronously
            await asyncio.sleep(0)

(TaskGroup requires Python 3.11 >)