-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjango_model_server.py
93 lines (71 loc) · 2.65 KB
/
django_model_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import time
from concurrent.futures import ThreadPoolExecutor
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.concurrent import run_on_executor
# django settings must be called before importing models
from utils import init_django
init_django()
from db._models import Schema
class OriginalAsyncHandler(tornado.web.RequestHandler):
"""Make the task non-blocking use python's(>=3.7) async and await"""
class Obj:
def __init__(self, pk, name, desc):
self.pk = pk
self.name = name
self.desc = desc
async def get(self):
print(1)
# await asyncio.sleep(0.99)
# time.sleep(5)
print(2)
# self.write("hello")
res = [self.Obj(i, "a", "b") for i in range(10)]
print(res)
self.render(
"templates/index.html",
items=res,
)
class BlockingToAsyncHandler(tornado.web.RequestHandler):
"""Make the task non-blocking, Supports the use of other libraries in code that do not support synchronization.
**Asynchronous Programming Based on Thread Pool**
If the other libraries being used, such as `time` and `urllib`, do not support asynchronous operations, the response will still be blocking.
In Tornado, there is a decorator that can use `ThreadPoolExecutor` to turn blocking processes into non-blocking ones. The principle is to start a separate thread outside of Tornado's own thread to execute the blocking program, thereby making Tornado non-blocking.
"""
# Must define an executor thread pool for decorator run_on_executor to work
executor = ThreadPoolExecutor(max_workers=10)
# Use @run_on_executor to make the task non-blocking, but need to create an executor first
@run_on_executor
def task(self):
time.sleep(1)
# url = "https://www.google.com/"
# url = "https://www.baidu.com/"
# import urllib.request
# res = urllib.request.urlopen(url).read()
# self.write('res')
res = Schema.objects.all()
return res
# Use @tornado.gen.coroutine
@tornado.gen.coroutine
def get(self):
print("-> get enter")
# 2. use yield
res = yield self.task()
print("<- get outer", res)
# self.write(res)
self.render(
"templates/index.html",
items=res,
)
# map the Urls to the class
application = tornado.web.Application(
[
(r"/", BlockingToAsyncHandler),
]
)
# Start the server
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()