diff options
| author | Allan Feldman <afeldman@newrelic.com> | 2021-06-30 17:37:10 +0200 |
|---|---|---|
| committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-07-01 12:13:19 +0200 |
| commit | 36fa071d6ebd18a61c4d7f1b5c9d17106134bd44 (patch) | |
| tree | 91289a6651b30587b212b0a1d96c14dc40faf103 /tests/asgi/urls.py | |
| parent | 4af162d4de5d60cef42e4707d821c1d6c0c99be0 (diff) | |
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.
Diffstat (limited to 'tests/asgi/urls.py')
| -rw-r--r-- | tests/asgi/urls.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/asgi/urls.py b/tests/asgi/urls.py index ff8d21ea7c..22d85604d1 100644 --- a/tests/asgi/urls.py +++ b/tests/asgi/urls.py @@ -1,3 +1,5 @@ +import threading + from django.http import FileResponse, HttpResponse from django.urls import path @@ -14,6 +16,18 @@ def hello_meta(request): ) +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__ @@ -21,4 +35,5 @@ urlpatterns = [ path('', hello), path('file/', lambda x: FileResponse(open(test_filename, 'rb'))), path('meta/', hello_meta), + path('wait/', sync_waiter), ] |
