#๐Ÿ”’ What exactly Happens here

47 messages ยท Page 1 of 1 (latest)

hot axle
#
for t in tasks:
t.cancel()
group = asyncio.gather(*tasks, return_exceptions=True) loop.run_until_complete(group) 

What exactly happens , here? Does the group task monitor all the tasks that were pending in the loop , if so what do we need the loop.run_until_complete for?

warm cargoBOT
#

@hot axle

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.

karmic flare
#

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.

hot axle
#

so gather is waiting for the tasks to complete the cleanup ?

karmic flare
#

To exit the event loop, yes.

#

There is an alternative thing for that, by the way.

#

!d asyncio.TaskGroup

warm cargoBOT
#

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.
hot axle
#

then why does it hav e to run run_until_complete again?

karmic flare
#

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.

hot axle
#

yes... but it is used twice

#

i am asking about the second time

#

inside the event loop

karmic flare
hot axle
#

yes

karmic flare
#

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.

hot axle
karmic flare
# hot axle 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.

hot axle
#

can you please tell me what exactly happens from the moment i run the cancel in the for loop

karmic flare
#

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.

karmic flare
hot axle
#

gotcha

#

thankyou

#

do you have any suggestions for book on asyncio

#

i read half of calebs book

karmic flare
#

There is a great talk about the internal of asyncio. I'm not aware of any books on it.

hot axle
#

thanks alot

#

have a great day

warm cargoBOT
#
Python help channel closed using Discord native close action

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.