Skip to content
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

Update request-id recipe to use contextvars instead of threading.local() #2260

Open
vytas7 opened this issue Aug 8, 2024 · 2 comments
Open
Labels
documentation good first issue Comment on this issue if you'd like to volunteer to work on this. Thanks! maintenance needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks!

Comments

@vytas7
Copy link
Member

vytas7 commented Aug 8, 2024

contextvars is a newer module in the stdlib (3.7+) that is more advanced than theading.local() as it not only understand threads, but also asyncio coroutines, etc.

Update the Request ID Logging recipe to use contextvars.

Edit: also add tests for this recipe.

@vytas7 vytas7 added documentation good first issue Comment on this issue if you'd like to volunteer to work on this. Thanks! needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! maintenance labels Aug 8, 2024
@CaselIT
Copy link
Member

CaselIT commented Aug 9, 2024

I don't think there is any need to use threadlocal or contextvar. just use req or resp context.

re-reading all that page it proposes several approaches. so yes, context var is better than threadlocal

@vytas7
Copy link
Member Author

vytas7 commented Aug 10, 2024

Indeed, using global thread-locals is usually not a very bright idea and it leads to a spaghetti code base. But sometimes, particularly when working with a code base migrated from another framework that builds on this paradigm, or when very deep inside nested code, it might be a useful escape hatch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation good first issue Comment on this issue if you'd like to volunteer to work on this. Thanks! maintenance needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks!
Projects
None yet
Development

No branches or pull requests

2 participants