[Django] #32889: Use asgiref ThreadSensitiveContext to allow per-request sync threads.

148 views
Skip to first unread message

Django

unread,
Jun 29, 2021, 4:18:35 AM6/29/21
to django-...@googlegroups.com
#32889: Use asgiref ThreadSensitiveContext to allow per-request sync threads.
-------------------------------------+-------------------------------------
Reporter: Carlton | Owner: Allan Feldman
Gibson |
Type: | Status: assigned
Cleanup/optimization |
Component: HTTP | Version: 4.0
handling |
Severity: Normal | Keywords: async, asgi
Triage Stage: Accepted | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
[https://github.com/django/asgiref/pull/227/files# asgiref 3.3.2 added
ThreadSensitiveContext] allowing `sync_to_async(thread_sensitive=True)` to
be scoped per-request, rather than globally.

With Django 4.0 being 3.8+ the required `contextvars` module is available.

Adjust `ASGIHandler` to use `ThreadSensitiveContext`

[https://github.com/django/django/pull/13882 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/32889>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 30, 2021, 3:37:49 AM6/30/21
to django-...@googlegroups.com
#32889: Use asgiref ThreadSensitiveContext to allow per-request sync threads.
-------------------------------------+-------------------------------------
Reporter: Carlton Gibson | Owner: Allan
Type: | Feldman
Cleanup/optimization | Status: assigned
Component: HTTP handling | Version: 4.0
Severity: Normal | Resolution:
Keywords: async, asgi | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32889#comment:1>

Django

unread,
Jul 1, 2021, 6:33:32 AM7/1/21
to django-...@googlegroups.com
#32889: Use asgiref ThreadSensitiveContext to allow per-request sync threads.
-------------------------------------+-------------------------------------
Reporter: Carlton Gibson | Owner: Allan
Type: | Feldman
Cleanup/optimization | Status: closed

Component: HTTP handling | Version: 4.0
Severity: Normal | Resolution: fixed
Keywords: async, asgi | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton.gibson@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"36fa071d6ebd18a61c4d7f1b5c9d17106134bd44" 36fa071]:
{{{
#!CommitTicketReference repository=""
revision="36fa071d6ebd18a61c4d7f1b5c9d17106134bd44"
Fixed #32889 -- Allowed per-request sync_to_async context in ASGIHandler .

By using a asgiref's ThreadSensitiveContext context manager, requests
will be able to execute independently of other requests when sync work
is involved.

Prior to this commit, a single global thread was used to execute any
sync work independent of the request from which that work was scheduled.
This could result in contention for the global sync thread in the case
of a slow sync function.

Requests are now isolated to their own sync thread.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32889#comment:2>

Reply all
Reply to author
Forward
0 new messages