This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author achimnol
Recipients achimnol, asvetlov, gvanrossum, yselivanov
Date 2022-02-25.04:05:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1645761918.07.0.0118762835065.issue46843@roundup.psfhosted.org>
In-reply-to
Content
@gvanrossum As you mentioned, the event loop currently plays the role of the top-level task group already, even without introducing yet another top-level task.  For instance, asyncio.run() includes necessary shutdown procedures to cancel all belonging unfinished tasks and async generators.

However, I think we should provide an abstraction to organize the shutdown procedures in a *hierarchical* manner.  For example, we could cancel all event handler tasks before cancelling all HTTP handler tasks upon a web server shutdown.  This prevents any potential races between theses two different task sets.  I think you could agree with the necessity of orderly release of underlying resources during shutdown in general.  Currently asyncio.Task.all_tasks() is just a list created from WeakSet and we cannot guarantee which tasks will be cancelled first.

Yes, this can be done by manually writing codes to declare multiple WeakSets and a for-loop to cancel the contained tasks by enumerating over them, just like asyncio.run() does.  With the new addition of TaskGroup and ExceptionGroup, this code does not require core changes of Python.

But I believe that this hierarchical persistent task group abstraction should be an essential part of the API and asyncio tutorials when writing server applications.  asyncio.run() could be written by users, but I think the core devs have agreed with that it is an essential abstraction to be included in the stdlib.  I'd like argue that hierarchical persistent task groups is the same case.

Though I named it "PersistentTaskGroup" because it looks similar to TaskGroup, but this name may be misleading.  In PersistentTaskGroup, even when all tasks finish successfully, it does NOT terminate but keeps waiting for new tasks to be spawned.  It terminates only when the outer task is cancelled or its shutdown() method is called.  Note that belonging tasks may be either short-running or long-running, and this does not matter.  The point is to shutdown any remaining tasks in an orderly manner.  If you don't like the naming, please suggest alternatives.
History
Date User Action Args
2022-02-25 04:05:18achimnolsetrecipients: + achimnol, gvanrossum, asvetlov, yselivanov
2022-02-25 04:05:18achimnolsetmessageid: <1645761918.07.0.0118762835065.issue46843@roundup.psfhosted.org>
2022-02-25 04:05:18achimnollinkissue46843 messages
2022-02-25 04:05:17achimnolcreate