django-log-request-id icon indicating copy to clipboard operation
django-log-request-id copied to clipboard

Use `ContextVar` instead of `threading.local`

Open Azmisov opened this issue 1 year ago • 4 comments

I believe switching to using contextvar lib would allow tracking request ID through asynchronous contexts, like when using Daphne + ASGI

Azmisov avatar Dec 09 '24 20:12 Azmisov

see https://docs.python.org/3/library/contextvars.html#asyncio-support for example

Azmisov avatar Dec 09 '24 20:12 Azmisov

Hi,

We're already using asgiref.local.Local where available (asgiref is now a dependency of all supported versions of Django). See https://github.com/dabapps/django-log-request-id/blob/master/log_request_id/init.py#L7

asgiref uses contextvars internally: https://github.com/django/asgiref/blob/main/asgiref/local.py

j4mie avatar Dec 10 '24 11:12 j4mie

Okay, you can update your README. It still says you're using threading.local and asks for suggestions

Azmisov avatar Dec 10 '24 16:12 Azmisov

Oh good catch!

j4mie avatar Dec 10 '24 16:12 j4mie

fwiw, it still still says that :P

jseakle avatar Apr 28 '25 10:04 jseakle