async def test_thread_sensitive_nested_context(): result_1 = {} result_2 = {} @sync_to_async def store_thread(result): result["thread"] = threading.current_thread() async with ThreadSensitiveContext(): await store_thread(result_1) async with ThreadSensitiveContext(): await store_thread(result_2) # They should not have run in the main thread, and on the same threads assert result_1["thread"] != threading.current_thread() assert result_1["thread"] == result_2["thread"]
async def __call__(self, scope, receive, send): """ Async entrypoint - parses the request and hands off to get_response. """ # Serve only HTTP connections. # FIXME: Allow to override this. if scope["type"] != "http": raise ValueError( "Django can only handle ASGI/HTTP connections, not %s." % scope["type"] ) async with ThreadSensitiveContext(): await self.handle(scope, receive, send)
async def test_thread_sensitive_context_without_sync_work(): async with ThreadSensitiveContext(): pass
async def fn(): async with ThreadSensitiveContext(): # Run it (in supposed parallel!) await asyncio.wait( [store_thread_async(result_1), store_thread_async(result_2)])
async def fn(result): async with ThreadSensitiveContext(): await store_thread(result)