xref: /unit/test/python/threads/asgi.py (revision 1848)
11683Smax.romanov@nginx.comimport asyncio
21683Smax.romanov@nginx.comimport time
31683Smax.romanov@nginx.comimport threading
41683Smax.romanov@nginx.com
5*1848Szelenkov@nginx.com
61683Smax.romanov@nginx.comasync def application(scope, receive, send):
71683Smax.romanov@nginx.com    assert scope['type'] == 'http'
81683Smax.romanov@nginx.com
91683Smax.romanov@nginx.com    headers = scope.get('headers', [])
101683Smax.romanov@nginx.com
111683Smax.romanov@nginx.com    def get_header(n, v=None):
121683Smax.romanov@nginx.com        for h in headers:
131683Smax.romanov@nginx.com            if h[0] == n:
141683Smax.romanov@nginx.com                return h[1]
151683Smax.romanov@nginx.com        return v
161683Smax.romanov@nginx.com
171683Smax.romanov@nginx.com    delay = float(get_header(b'x-delay', 0))
181683Smax.romanov@nginx.com
191683Smax.romanov@nginx.com    time.sleep(delay)
201683Smax.romanov@nginx.com
21*1848Szelenkov@nginx.com    await send(
22*1848Szelenkov@nginx.com        {
23*1848Szelenkov@nginx.com            'type': 'http.response.start',
24*1848Szelenkov@nginx.com            'status': 200,
25*1848Szelenkov@nginx.com            'headers': [
26*1848Szelenkov@nginx.com                (b'content-length', b'0'),
27*1848Szelenkov@nginx.com                (b'x-thread', str(threading.currentThread().ident).encode()),
28*1848Szelenkov@nginx.com            ],
29*1848Szelenkov@nginx.com        }
30*1848Szelenkov@nginx.com    )
31