-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
✨ Add support for copying contextvars
to thread workers
#2160
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2160 +/- ##
==========================================
+ Coverage 99.57% 99.62% +0.04%
==========================================
Files 114 115 +1
Lines 14733 14861 +128
Branches 2335 2339 +4
==========================================
+ Hits 14671 14805 +134
+ Misses 41 38 -3
+ Partials 21 18 -3
|
I'm not sure what to do about the Fedora CI broken build 🤔 I imagine this also requires an update in release notes, etc. But I would like to get some feedback first, check if anything seems wrong to anyone here, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from my comments (one of which leaked out of this review oops!) I think the question of adding context kwargs to the API should be re-confirmed by a few others.
Thanks for the thorough review @richardsheridan! ☕ Yep, I would have probably not included explicit I'll wait for more feedback. 🤓 |
I merged master into your branch, this fixed the Fedora build! |
trio/_threads.py
Outdated
func = cb | ||
func_args = (q, fn, args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I realized I'm already modifying the two calls that use this function, it's never called without a context, so I just made the context
argument required, and that seems to have solved the coverage.
…rrent_async_library_cvar
Thanks for the reviews and comments @richardsheridan, @smurfix, and @pquentin! 🍰 I implemented the changes, tests, and updated the coverage. I also just added the I think this would cover all the things necessary for the PR, am I missing something else? |
Co-authored-by: richardsheridan <[email protected]>
Co-authored-by: richardsheridan <[email protected]>
Awesome, thanks for the review @richardsheridan! I agree, I would love to have @njsmith's point of view on this! |
If it were just me, I would merge the PR for the next release but without the kwarg change to |
I fully agree. @tiangolo would it be possible to modify this PR or create another one without the API changes? |
as requested in code review
Awesome, thanks for the feedback @richardsheridan and @agronholm! 🙇♂️ 🚀 Done, I updated it accordingly. Let me know if you see anything else or if I missed something. |
The failing tests will likely be fixed by merging master. |
@@ -1540,7 +1543,7 @@ def task_exited(self, task, outcome): | |||
################ | |||
|
|||
@_public | |||
def spawn_system_task(self, async_fn, *args, name=None): | |||
def spawn_system_task(self, async_fn, *args, name=None, context=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is supposed to be here anymore? System tasks should always inherit the system context, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually haven't been able to make tests pass without it, I've been trying several different approaches, copying the context in different places, but in some I get errors that show it's not copying the right context, or thread timeout errors, or it breaks internals. 😱 I suspect it could be just that I lack the knowledge of the internals to do it in a different and better way.
Do you see a way that could be solved to remove it?
On the other hand, I understand @richardsheridan thinks this could be useful... for me it's the same, but I just don't see how to make it work without this. 🤔
I think that's a good feature, otherwise there is no way to inject any
other context to a system task...
…On Sun, Jan 30, 2022, 11:10 AM Alex Grönholm ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In trio/_core/_run.py
<#2160 (comment)>:
> @@ -1540,7 +1543,7 @@ def task_exited(self, task, outcome):
################
@_public
- def spawn_system_task(self, async_fn, *args, name=None):
+ def spawn_system_task(self, async_fn, *args, name=None, context=None):
I don't think this is supposed to be here anymore? System tasks should
always inherit the system context, right?
—
Reply to this email directly, view it on GitHub
<#2160 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB5BHRSUGNPVHRB3ACB37SLUYVPGLANCNFSM5G3IHZUQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I just noticed this warning in But I can't see what generates it, the only reference to |
Right you should run |
That API addition is not mentioned in the news fragment. |
@agronholm get it, just updated it. I suspect only about |
Thanks @pquentin! 🚀 |
This PR introduced a regression: calling |
For my future self's reference: This was regression resulted in #2183 and was fixed in #2462. |
✨ Add support for copying
contextvars
to thread workers.This is my attempt at implementing what's described here: #648
The original post said:
I looked at the release notes and realized these names have been deprecated/replaced by other things:
run_sync_in_worker_thread
->to_thread.run_sync
BlockingTrioPortal
->from_thread.run_sync
andfrom_thread.run
I'm also assuming that the "
sniffio
magic contextvar" refers tosniffio.current_async_library_cvar
.