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