-
-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document that Nursery.start_soon starts tasks in random order #970
Comments
Yes, this is intentional. They are started in FIFO order, in the sense that first task 1 is created and marked as runnable, then task 2, then task 3. And then the next time the parent task goes to sleep, the scheduler picks which task to run next. At this point those 3 tasks are all runnable – and in a more complex program, there might be arbitrary other tasks that were runnable as well – and the scheduler picks between them. And in general, Trio's scheduler doesn't make any guarantees about when it will run different tasks; all it guarantees is that all tasks will get a fair chance to run. In practice, this is about the strongest guarantee you can usefully get from a scheduler. If you run tasks concurrently, then they run at different rates :-). In some cases you might be able to manually line up their execution in some consistent way for a while, but it's going to be quite fragile. You might also find #32 and #239 interesting. If you want to make sure that task 1 has had a chance to finish starting up before starting task 2, then see Where would you have expected to see this in the docs? |
That makes sense. My naive expectation was that, because the docs emphasized that control gets nondeterministic at checkpoints, tasks would run in order up until the first checkpoint, at which point things would get wibbly-wobbly. I would expect to see this documented at |
I guess technically that's true – the child tasks don't get a chance to run at all until the parent task (the one that called
That makes sense. Would you be interested in submitting a patch to update the text? It's in |
Thanks. I think I can give it a whack.
|
Awesome!
Yeah, currently Trio does make sure that all runnable tasks get the same number of checkpoints, and your observation is a side-effect of that. This is a somewhat crude attempt to control fairness, and make sure that all tasks make some progress. It might change in the future though... it's workable, but it tends to penalize tasks that checkpoint a lot versus tasks that only checkpoint occasionally. If we can come up with something better then we reserve the right to switch to it :-) |
One more question. Is it dependable to flush unstarted nursery tasks with |
@rotu Depends on what you mean by 'flush unstarted tasks'. Currently if you call The word "soon" in But, the nursery closing definitely doesn't affect anything, except that it's a where the parent task goes to sleep (= checkpoints) and the scheduler is free to run other stuff. |
Hi,
No it isn't. A checkpoint is presumed to let at least one other runnable task proceed before it continues, and there are too many checkpoints in This code demonstrates randomness:
|
That makes sense. Why does moving the print statement out of the loop (just outdent |
Because Trio batches the runnable tasks, so that none gets starved indefinitely. The overall effect is that while you get some reordering, no task can skip ahead / lag behind too far, and they still end up terminating in the same order they started in. |
I came to trio with the same expectation as the OP, and was confused in exactly the same way. If multiple tasks begin with entering a fifo lock, entering a capacity limiter context, or sending to a channel, but then continue independently, it's quite often useful to have a number of them begin in a defined order. Immediately calling |
Isn't it quite trivial to write that helper function when you need it?
|
It might be trivial, but appears to be useful enough to become part of |
@bergus Would you expect In any case, I think giving "the first checkpoint" special barrier semantics would be fragile and error-prone: what if you need to refactor and now your code does two checkpoints while grabbing the fifo lock? With Writing |
When calling
Nursery.start_soon
, submitted tasks are called in an apparently non-deterministic order. This is not intuitive (at least to me); sincenursery.start_soon
is synchronous I expected the functions to be started in FIFO order. If the order is non-deterministic on purpose, this should be documented.Toy example:
The text was updated successfully, but these errors were encountered: