#๐ What exactly Happens here
47 messages ยท Page 1 of 1 (latest)
@hot axle
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.
run_until_complete generally means running a coroutine or a task within a specified loop. This is where you actually run all the tasks. Whereas group is a task/coroutine responsible for waiting for all the tasks to complete.
Since tasks there are already running while loop is active, group is only to wait for those tasks to complete.
Without group, loop doesn't know when to stop running.
Another option is to loop.run_forever and have something loop.stop(), but that'd be quite complicated.
so gather is waiting for the tasks to complete the cleanup ?
To exit the event loop, yes.
There is an alternative thing for that, by the way.
!d asyncio.TaskGroup
class asyncio.TaskGroup```
An [asynchronous context manager](https://docs.python.org/3/reference/datamodel.html#async-context-managers) holding a group of tasks. Tasks can be added to the group using [`create_task()`](https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task). All tasks are awaited when the context manager exits.
Added in version 3.11.
then why does it hav e to run run_until_complete again?
run_until_complete runs the event loop itself. This is what asyncio.run uses too.
Before you call run_until_complete, no tasks are running yet, even if they were scheduled.
The example you sent doesn't have that. Can you provide broader context?
yes
Is this code happening inside an async def?
Likely you just need to await group if that is the case.
loop.run_until_complete and asyncio.run can be thought of as an await equivalent which can be called outside an async def.
here
Ah, okay. In this case you have to involve it the second time because it got interrupted earlier.
Signal interrupts the event loop, you catch it, then go back into it to cleanup.
Alternative approach is to save this task, then cancel it directly.
can you please tell me what exactly happens from the moment i run the cancel in the for loop
Just before you interrupt the program, this thing is running
# < Here, we are outside an event loop.
loop.run_forever() # Here, we are in an event loop.
# < Here, we are outside an event loop.
All the tasks are being executed there until an interrupt.
Next like I think is already self-describing.
Here, we cancel all tasks which raises a CancelledError inside them.
for t in tasks
t.cancel()
But that exception hasn't yet hit any code inside the tasks.
They can't yet catch it and handle it, since they aren't running.
(Because we exited the event loop)
Once we raised that exception in all tasks, we come back into the event loop.
group = ...
loop.run_until_complete(group)
asyncio.group just waits until everything you gave it is done. You can think of it as making one task from many tasks.
Inside that run_until_complete, remaining tasks are run until completion.
gotcha
thankyou
do you have any suggestions for book on asyncio
i read half of calebs book
There is a great talk about the internal of asyncio. I'm not aware of any books on it.
Screencast based on a workshop originally presented at PyCon India, Chennai, October 14, 2019.
Code samples at: https://gist.github.com/dabeaz/f86ded8d61206c757c5cd4dbb5109f74
This workshop is about the low-level foundations and abstractions for asynchronous programming in Python. It's a bit unusual in that rather than starting with tradi...
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.