mirror of
https://github.com/django/django.git
synced 2024-12-24 10:05:46 +00:00
36fa071d6e
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.
40 lines
890 B
Python
40 lines
890 B
Python
import threading
|
|
|
|
from django.http import FileResponse, HttpResponse
|
|
from django.urls import path
|
|
|
|
|
|
def hello(request):
|
|
name = request.GET.get('name') or 'World'
|
|
return HttpResponse('Hello %s!' % name)
|
|
|
|
|
|
def hello_meta(request):
|
|
return HttpResponse(
|
|
'From %s' % request.META.get('HTTP_REFERER') or '',
|
|
content_type=request.META.get('CONTENT_TYPE'),
|
|
)
|
|
|
|
|
|
def sync_waiter(request):
|
|
with sync_waiter.lock:
|
|
sync_waiter.active_threads.add(threading.current_thread())
|
|
sync_waiter.barrier.wait(timeout=0.5)
|
|
return hello(request)
|
|
|
|
|
|
sync_waiter.active_threads = set()
|
|
sync_waiter.lock = threading.Lock()
|
|
sync_waiter.barrier = threading.Barrier(2)
|
|
|
|
|
|
test_filename = __file__
|
|
|
|
|
|
urlpatterns = [
|
|
path('', hello),
|
|
path('file/', lambda x: FileResponse(open(test_filename, 'rb'))),
|
|
path('meta/', hello_meta),
|
|
path('wait/', sync_waiter),
|
|
]
|