xref: /unit/test/python/threads/asgi.py (revision 1971:3410f9d2a662)
1import threading
2import time
3
4
5async def application(scope, receive, send):
6    assert scope['type'] == 'http'
7
8    headers = scope.get('headers', [])
9
10    def get_header(n, v=None):
11        for h in headers:
12            if h[0] == n:
13                return h[1]
14        return v
15
16    delay = float(get_header(b'x-delay', 0))
17
18    time.sleep(delay)
19
20    await send(
21        {
22            'type': 'http.response.start',
23            'status': 200,
24            'headers': [
25                (b'content-length', b'0'),
26                (b'x-thread', str(threading.currentThread().ident).encode()),
27            ],
28        }
29    )
30