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.
await asyncio.sleep(0)in the for-loop ofmanager. This allows to suspendmanagerand to execute tasks.manageris only halted temporarily to execute tasks and then the execution is resumed. Ifmanagernever halts=awaits something (as it is currently), no tasks can be executed in the same event loop.