-
Notifications
You must be signed in to change notification settings - Fork 146
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
to/from_thread and contextvars #363
Comments
Which do you feel would be the correct way of propagating context? |
I think the most intuitive would be that both |
Having thought about this some more, I agree. It seems though that I cannot force trio to do this when calling back to async-land from a worker thread. Asyncio, on the other hand, behaves this way naturally. |
Then maybe we should add that feature to Trio. |
I was able to do this even on trio (thanks @richardsheridan) but it was a very ugly workaround. Getting this sorted out on trio would therefore be a welcome change. |
linking a relevant PR: python-trio/trio#2160 |
I thought I had a PR ready for this but then I started testing contextvar propagation with |
I have a PR for this here: #389. I started the work in Trio in python-trio/trio#2160 with the ultimate objective of having this in AnyIO. 🤓 |
The behavior of how contextvars are propagated through
to
/from_thread
differs depending on which backend you use. Withasyncio
, they inherit the context of the calling thread; intrio
they inherit the context of the system task group.On anyio 3.3.0, this prints:
This is easy (but inconvenient) to work around for
run_sync
by adding acopy_context().run
, but I think any similar workaround forrun
would require special-casing for trio.Also,
asyncio.to_thread
copies the context whereasanyio.to_thread.run_sync
does not:This is not as important since they are different functions, but it's still annoying. Same workaround as above works here, of course.
The text was updated successfully, but these errors were encountered: