TaskGroup.start() like anyio has. #119
Unanswered
jonathanslenders
asked this question in
Questions
Replies: 1 comment
-
I just realized we can do better in anyio too: agronholm/anyio#678 It's totally doable to offer a strictly typed |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Follow-up of: https://twitter.com/tiangolo/status/1750533650201154032
Anyio has a
TaskGroup.start()
function alongsideTaskGroup.start_soon()
: https://anyio.readthedocs.io/en/stable/api.html#anyio.abc.TaskGroup.start. This allows for starting a task in a task group and wait for it to signal readiness.This is really an important feature in structured concurrency. Because, sometimes certain operations, even when they are concurrent, are only supposed to start when other concurrent tasks are past a certain point of readiness. E.g., one task might start a web server, and the main task will wait for the web server to actually have opened a port for listening before proceeding.
I noticed that asyncer has a
soonify()
which corresponds tostart_soon
, but it does not have astartify
or anything that would correspond tostart_soon
. You might think that instead passing an event to the task for it to signal readiness would be sufficient, but it's not. With an event, we would end up with the following:That's already three lines of boilerplace to make
TaskGroup.start()
working, but it doesn't cover everything. If something happens and the task returns (without exception) before the event was set, then this code will hang forever.The big problem from a typing point of view is that
start()
takes aTaskStatus
, as you can see here: https://anyio.readthedocs.io/en/stable/tasks.html#starting-and-initializing-tasksThat means, one argument is passed in by the
start()
helper itself, and the other arguments are passed in by the caller of the function.Look at the anyio source code.
TaskStatus.start()
is in fact already fully typed since anyio 4.0, thanks totyping.Unpack
:https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_tasks.py#L49
So, asyncer doesn't add much beyond support for keyword arguments.
TaskStatus.start()
on the other hand is not very much typed, see: https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_tasks.py#L66So, to summarize, there are actually two points here:
TaskGroup.start
is important to have, it simplifies a lot of code.TaskGroup.start
seems non-trivial. I can't think of a way to reshape the call and make the typing work with paramspec/unpack/etc... as asyncer does withsoonify()
Beta Was this translation helpful? Give feedback.
All reactions