#πŸ”’ Running an `async` function in the background at the same time as going forward in `async for` loo

9 messages Β· Page 1 of 1 (latest)

indigo trout
#

Suppose that I have an async for loop of this form:

```async for x in f():
await g(x)```

In this case, the program will wait until g(x) is finished before going on to the next iteration of the for loop. What if I want to do it in parallell? I know that one can use asyncio.gather to run two async tasks in parallell, and is there then some way to run g(x) in parallel with the rest of the loop? Note that I can't simply use asyncio.gather on "all elements of f()", whatever that would mean, because I don't necessarily have all those elements from the start.

fickle hedgeBOT
#

@indigo trout

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sudden hornet
#

Is the next x reliant on the previous being computed?
What is preventing you from having all x on hand?

storm fox
#

I'd use create_task here

#

e.g.

tasks = []
async for x in f():
    tasks.append(asyncio.create_task(g(x))) # each g(x) gets to start right away
await asyncio.gather(*tasks) # wait for them all to finish
#

I think nowadays you can use a TaskGroup instead, but I still haven't quite learned how that works

sudden hornet
# indigo trout Suppose that I have an async for loop of this form: ```async for x in f(): ...

Tasks would be great for this
If you are interested in the Task group Reptile mentioned, I would suggest reading up on it
https://superfastpython.com/asyncio-taskgroup/

A problem with tasks is that it is a good idea to assign and keep track of the asyncio.Task objects. The reason is that if we don’t the tasks may be garbage collected, terminating the task. A helpful solution is to use a TaskGroup to create and manage a collection of tasks. It has a […]

fickle hedgeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.