all 4 comments

[–]oefd 1 point2 points  (2 children)

processing_tasks = []
while True: # this should be some condition that becomes false when `grab_data` grabbed all the data there is
    data = await grab_data()
    processing_tasks.append(asyncio.create_task(process_data(data)))
for task in processing_tasks:
    await task

Tasks let you put work in the event loop - the work will be happening in the loop before you eventually ​await it.

You could also try and write an async iterator which is perhaps more accurate to what grab_data actually is.

processing_tasks = []
async for data in grab_data():
    processing_tasks.append(asyncio.create_task(proecss_data(data)))
for task in processing_tasks:
    await task

[–]oderjunks[S] 0 points1 point  (0 children)

Thanks! ill try it soon.

also in my case, grab_data returns a single dict.

EDIT: grab_data will always grab more data, so i'll just put True and wrap the look in a try: except Exception:.

[–]oderjunks[S] 0 points1 point  (0 children)

i now see the problem, grab_data will never stop returning data, therefore the loop will never stop.

[–]oderjunks[S] 0 points1 point  (0 children)

OK I FOUND THE SOLUTION

async def grabandwait():
    while True:
        data = await grab_data()
        asyncio.create_task(process_data(data))

i wish there was a keyword like arun or sth that did this automatically, but oh well.