Пример #1
0
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"]
Пример #2
0
    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)
Пример #3
0
async def test_thread_sensitive_context_without_sync_work():
    async with ThreadSensitiveContext():
        pass
Пример #4
0
 async def fn():
     async with ThreadSensitiveContext():
         # Run it (in supposed parallel!)
         await asyncio.wait(
             [store_thread_async(result_1),
              store_thread_async(result_2)])
Пример #5
0
 async def fn(result):
     async with ThreadSensitiveContext():
         await store_thread(result)