xref: /unit/test/python/threading/asgi.py (revision 1971:3410f9d2a662)
1import sys
2import threading
3import time
4
5
6class Foo(threading.Thread):
7    num = 10
8
9    def __init__(self, x):
10        self.__x = x
11        threading.Thread.__init__(self)
12
13    def log_index(self, index):
14        sys.stderr.write(
15            "(" + str(index) + ") Thread: " + str(self.__x) + "\n"
16        )
17        sys.stderr.flush()
18
19    def run(self):
20        i = 0
21        for _ in range(3):
22            self.log_index(i)
23            i += 1
24            time.sleep(1)
25            self.log_index(i)
26            i += 1
27
28
29async def application(scope, receive, send):
30    assert scope['type'] == 'http'
31
32    Foo(Foo.num).start()
33    Foo.num += 10
34
35    await send(
36        {
37            'type': 'http.response.start',
38            'status': 200,
39            'headers': [(b'content-length', b'0')],
40        }
41    )
42