Skip to content

Commit 354c68f

Browse files
committed
add tornado web service.
1 parent a63bc16 commit 354c68f

29 files changed

Lines changed: 972 additions & 19 deletions

01base/15.async.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
8+
def demo4():
9+
"""
10+
这是最终我们想要的实现.
11+
"""
12+
import asyncio # 引入 asyncio 库
13+
14+
async def washing1():
15+
await asyncio.sleep(3) # 使用 asyncio.sleep(), 它返回的是一个可等待的对象
16+
print('washer1 finished')
17+
18+
async def washing2():
19+
await asyncio.sleep(2)
20+
print('washer2 finished')
21+
22+
async def washing3():
23+
await asyncio.sleep(5)
24+
print('washer3 finished')
25+
26+
"""
27+
事件循环机制分为以下几步骤:
28+
1. 创建一个事件循环
29+
2. 将异步函数加入事件队列
30+
3. 执行事件队列, 直到最晚的一个事件被处理完毕后结束
31+
4. 最后建议用 close() 方法关闭事件循环, 以彻底清理 loop 对象防止误用
32+
"""
33+
# 1. 创建一个事件循环
34+
loop = asyncio.get_event_loop()
35+
36+
# 2. 将异步函数加入事件队列
37+
tasks = [
38+
washing1(),
39+
washing2(),
40+
washing3(),
41+
]
42+
43+
# 3. 执行事件队列, 直到最晚的一个事件被处理完毕后结束
44+
loop.run_until_complete(asyncio.wait(tasks))
45+
"""
46+
PS: 如果不满意想要 "多洗几遍", 可以多写几句:
47+
loop.run_until_complete(asyncio.wait(tasks))
48+
loop.run_until_complete(asyncio.wait(tasks))
49+
loop.run_until_complete(asyncio.wait(tasks))
50+
...
51+
"""
52+
53+
# 4. 如果不再使用 loop, 建议养成良好关闭的习惯
54+
# (有点类似于文件读写结束时的 close() 操作)
55+
loop.close()
56+
57+
"""
58+
最终的打印效果:
59+
washer2 finished
60+
washer1 finished
61+
washer3 finished
62+
elapsed time = 5.126561641693115
63+
(毕竟切换线程也要有点耗时的)
64+
65+
说句题外话, 我看有的博主的加入事件队列是这样写的:
66+
tasks = [
67+
loop.create_task(washing1()),
68+
loop.create_task(washing2()),
69+
loop.create_task(washing3()),
70+
]
71+
运行的效果是一样的, 暂不清楚为什么他们这样做.
72+
"""
73+
74+
75+
if __name__ == '__main__':
76+
# 为验证是否真的缩短了时间, 我们计个时
77+
from time import time
78+
start = time()
79+
80+
# demo1() # 需花费10秒
81+
# demo2() # 会报错: RuntimeWarning: coroutine ... was never awaited
82+
# demo3() # 会报错: RuntimeWarning: coroutine ... was never awaited
83+
demo4() # 需花费5秒多一点点
84+
85+
end = time()
86+
print('elapsed time = ' + str(end - start))

20pytorch/04.cifar10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def forward(self, x):
8181
loss = criterion(outputs, labels)
8282
loss.backward()
8383
optimizer.step()
84-
running_loss += loss.data[0]
84+
running_loss += loss.data.item()
8585
if i % 2000 == 1999:
8686
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
8787
running_loss = 0.0

20pytorch/13.transformer_translate.ipynb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
"import matplotlib.pyplot as plt\n",
5555
"import matplotlib.ticker as ticker\n",
5656
"\n",
57-
"import spacy\n",
5857
"import numpy as np\n",
5958
"\n",
6059
"import random\n",
@@ -116,17 +115,17 @@
116115
"metadata": {},
117116
"outputs": [],
118117
"source": [
119-
"SRC = Field(tokenize = tokenize_de, \n",
120-
" init_token = '<sos>', \n",
121-
" eos_token = '<eos>', \n",
122-
" lower = True, \n",
123-
" batch_first = True)\n",
118+
"SRC = Field(tokenize=tokenize_de,\n",
119+
" init_token='<sos>',\n",
120+
" eos_token='<eos>',\n",
121+
" lower=True,\n",
122+
" batch_first=True)\n",
124123
"\n",
125-
"TRG = Field(tokenize = tokenize_en, \n",
126-
" init_token = '<sos>', \n",
127-
" eos_token = '<eos>', \n",
128-
" lower = True, \n",
129-
" batch_first = True)"
124+
"TRG = Field(tokenize=tokenize_en,\n",
125+
" init_token='<sos>',\n",
126+
" eos_token='<eos>',\n",
127+
" lower=True,\n",
128+
" batch_first=True)"
130129
]
131130
},
132131
{
@@ -142,8 +141,8 @@
142141
"metadata": {},
143142
"outputs": [],
144143
"source": [
145-
"train_data, valid_data, test_data = Multi30k.splits(exts = ('.de', '.en'), \n",
146-
" fields = (SRC, TRG))"
144+
"train_data, valid_data, test_data = Multi30k.splits(exts=('.de', '.en'),\n",
145+
" fields=(SRC, TRG))"
147146
]
148147
},
149148
{
@@ -169,7 +168,8 @@
169168
"metadata": {},
170169
"outputs": [],
171170
"source": [
172-
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')"
171+
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
172+
"print('device:', device)"
173173
]
174174
},
175175
{
@@ -1589,7 +1589,7 @@
15891589
"name": "python",
15901590
"nbconvert_exporter": "python",
15911591
"pygments_lexer": "ipython3",
1592-
"version": "3.7.6"
1592+
"version": "3.6.6"
15931593
}
15941594
},
15951595
"nbformat": 4,

24web/tornado_demo/01demo.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
import tornado
8+
9+
print(tornado.version_info)
10+
11+
import tornado.ioloop
12+
import tornado.web
13+
import tornado.httpserver
14+
15+
class HandleDemo(tornado.web.RequestHandler):
16+
def get(self, *args, **kwargs):
17+
self.write("hello")
18+
19+
20+
class CalcMin(tornado.web.RequestHandler):
21+
def get(self, *args, **kwargs):
22+
a = 123
23+
b = 321
24+
c = a * b
25+
self.write("%s * %s = %s" % (a, b, c))
26+
27+
28+
def make_app():
29+
return tornado.web.Application([
30+
(r"/", HandleDemo),
31+
(r"/calc", CalcMin),
32+
])
33+
34+
35+
if __name__ == '__main__':
36+
app = make_app()
37+
app.listen(9999)
38+
39+
# 不建议这个多进程,原因是绑定在一个端口,无法有效监控。
40+
# http_server = tornado.httpserver.HTTPServer(app)
41+
# http_server.bind(9999)
42+
# http_server.start(0) # cpu processor
43+
44+
tornado.ioloop.IOLoop.current().start()

24web/tornado_demo/02opt_cmd.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
import tornado
8+
import tornado.ioloop
9+
import tornado.httpserver
10+
import tornado.options
11+
import tornado.web
12+
tornado.options.define("port", default=9998, type=int, help="server port")
13+
tornado.options.define("names", default=['lili', 'lucy'], type=str, multiple=True, help="names")
14+
15+
class IndexHandle(tornado.web.RequestHandler):
16+
def get(self, *args, **kwargs):
17+
self.write("hello names")
18+
19+
if __name__ == '__main__':
20+
tornado.options.parse_command_line()
21+
print(tornado.options.options.names)
22+
app = tornado.web.Application([
23+
(r"/", IndexHandle),
24+
])
25+
server = tornado.httpserver.HTTPServer(app)
26+
server.listen(tornado.options.options.port)
27+
tornado.ioloop.IOLoop.current().start()

24web/tornado_demo/03opt_file.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
import tornado
8+
import tornado.ioloop
9+
import tornado.httpserver
10+
import tornado.options
11+
import tornado.web
12+
tornado.options.define("port", default=9998, type=int, help="server port")
13+
tornado.options.define("names", default=['lili', 'lucy'], type=str, multiple=True, help="names")
14+
15+
class IndexHandle(tornado.web.RequestHandler):
16+
def get(self, *args, **kwargs):
17+
self.write("hello names file")
18+
from tornado.options import options, parse_command_line
19+
options.logging = None
20+
parse_command_line()
21+
if __name__ == '__main__':
22+
tornado.options.parse_config_file('./config.ini')
23+
print(tornado.options.options.names)
24+
app = tornado.web.Application([
25+
(r"/", IndexHandle),
26+
])
27+
server = tornado.httpserver.HTTPServer(app)
28+
server.listen(tornado.options.options.port)
29+
tornado.ioloop.IOLoop.current().start()

24web/tornado_demo/04opt_py.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
import tornado.web
7+
import tornado
8+
from tornado_demo import config
9+
import tornado.ioloop
10+
import tornado.httpserver
11+
12+
13+
class IndexHandle(tornado.web.RequestHandler):
14+
def get(self, *args, **kwargs):
15+
self.write("hello names file")
16+
17+
18+
if __name__ == '__main__':
19+
app = tornado.web.Application([
20+
(r"/", IndexHandle),
21+
])
22+
http_server = tornado.httpserver.HTTPServer(app)
23+
http_server.bind(config.options["port"])
24+
http_server.start(1)
25+
tornado.ioloop.IOLoop.current().start()

24web/tornado_demo/05router.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
import tornado.web
8+
import tornado.ioloop
9+
import tornado.httpserver
10+
import tornado.options
11+
from tornado.options import options, define
12+
from tornado.web import url, RequestHandler
13+
14+
define("port", default=8000, type=int, help="run server on the given port.")
15+
16+
17+
class IndexHandler1(RequestHandler):
18+
def get(self):
19+
python_url = self.reverse_url("python_url")
20+
self.write('<a href="%s">itcast</a>' %
21+
python_url)
22+
23+
24+
class ItcastHandler(RequestHandler):
25+
def initialize(self, subject):
26+
self.subject = subject
27+
28+
29+
def get(self):
30+
self.write(self.subject)
31+
32+
33+
if __name__ == "__main__":
34+
tornado.options.parse_command_line()
35+
app = tornado.web.Application([
36+
(r"/", IndexHandler1),
37+
(r"/cpp", ItcastHandler, {"subject": "c++"}),
38+
url(r"/python", ItcastHandler, {"subject": "python"}, name="python_url")
39+
],
40+
debug=True)
41+
http_server = tornado.httpserver.HTTPServer(app)
42+
http_server.listen(options.port)
43+
tornado.ioloop.IOLoop.current().start()

24web/tornado_demo/06argument.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
import tornado.web
8+
import tornado.ioloop
9+
import tornado.httpserver
10+
import tornado.options
11+
from tornado.options import options, define
12+
from tornado.web import RequestHandler, MissingArgumentError
13+
14+
define("port", default=8000, type=int, help="run server on the given port.")
15+
16+
17+
class IndexHandler(RequestHandler):
18+
def post(self):
19+
query_arg = self.get_query_argument("a")
20+
query_args = self.get_query_arguments("a")
21+
body_arg = self.get_body_argument("a")
22+
body_args = self.get_body_arguments("a", strip=False)
23+
arg = self.get_argument("a")
24+
args = self.get_arguments("a")
25+
26+
default_arg = self.get_argument("b", "itcast")
27+
default_args = self.get_arguments("b")
28+
29+
try:
30+
missing_arg = self.get_argument("c")
31+
except MissingArgumentError as e:
32+
missing_arg = "We catched the MissingArgumentError!"
33+
print(e)
34+
missing_args = self.get_arguments("c")
35+
36+
rep = "query_arg:%s<br/>" % query_arg
37+
rep += "query_args:%s<br/>" % query_args
38+
rep += "body_arg:%s<br/>" % body_arg
39+
rep += "body_args:%s<br/>" % body_args
40+
rep += "arg:%s<br/>" % arg
41+
rep += "args:%s<br/>" % args
42+
rep += "default_arg:%s<br/>" % default_arg
43+
rep += "default_args:%s<br/>" % default_args
44+
rep += "missing_arg:%s<br/>" % missing_arg
45+
rep += "missing_args:%s<br/>" % missing_args
46+
47+
self.write(rep)
48+
49+
50+
if __name__ == "__main__":
51+
tornado.options.parse_command_line()
52+
app = tornado.web.Application([
53+
(r"/", IndexHandler),
54+
])
55+
http_server = tornado.httpserver.HTTPServer(app)
56+
http_server.listen(options.port)
57+
tornado.ioloop.IOLoop.current().start()

0 commit comments

Comments
 (0)