Opened 3 years ago

Closed 3 years ago

#32889 closed Cleanup/optimization (fixed)

Use asgiref ThreadSensitiveContext to allow per-request sync threads.

Reported by: Carlton Gibson Owned by: Allan Feldman
Component: HTTP handling Version: 4.0
Severity: Normal Keywords: async, asgi
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

PR

Change History (2)

comment:1 by Carlton Gibson, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:2 by Carlton Gibson <carlton.gibson@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 36fa071:

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.

Note: See TracTickets for help on using tickets.
Back to Top