您的位置:首页 > 编程语言 > Python开发

关于并发,异步,非阻塞(python)疑惑的一些资料解答

2017-12-28 17:15 417 查看
从iterable/iterator到generator到coroutine
理解python的迭代器: http://python.jobbole.com/81916/ 理解python的生成器: http://python.jobbole.com/81911/ python中协程:http://python.jobbole.com/87156/

从yield/send到yield from/asycio到asyc/awati
Python协程从yield/send到async/await: http://python.jobbole.com/86069/ 深入理解 Python 异步编程(上):http://python.jobbole.com/88291/

服务器是如何承受大量的用户请求的:https://www.zhihu.com/question/27629526
flask是如何处理多个访问请求的:https://segmentfault.com/q/1010000004532745
tornado 异步笔记:https://www.jianshu.com/p/31fae7dd05ba

tornado和flask异步的例子,请求非阻塞

1 #-*-coding:utf8-*-
2
3 import tornado.httpserver
4 import tornado.ioloop
5 import tornado.options
6 import tornado.web
7
8 import time
9 import os
10 import urllib.request
11 from tornado.options import define,options
12 define("port",default=8000,help="run on the given port",type=int)
13
14
15 class Async1Handler(tornado.web.RequestHandler):
16     @tornado.web.asynchronous
17     @tornado.gen.coroutine
18     def get(self,*args,**kwargs):
19         print("begin!")
20         response = yield tornado.gen.Task(self.open)
21         print(response)
22         self.finish("it works")
23
24     @tornado.gen.coroutine
25     def open(self):
26
27         return (urllib.request.urlopen('http://127.0.0.1:5000/asyn/'))
28         #os.system("ping -c 5 127.0.0.1")
29
30 if __name__ == "__main__":
31     tornado.options.parse_command_line()
32     print("****listening 127.0.0.1:8000****")
33     app = tornado.web.Application(
34                 handlers=[(r'/async',Async1Handler)],
35                 debug="True")
36     http_server = tornado.httpserver.HTTPServer(app)
37     http_server.listen(options.port)
38     tornado.ioloop.IOLoop.instance().start()


1 # coding=utf-8
2 # Python Version: 3.5.1
3
4 # Flask
5 from flask import Flask, request, g
6 import urllib.request
7
8 import os
9 from gevent import monkey
10 from gevent.pywsgi import WSGIServer
11 monkey.patch_all()
12
13
14 import time
15
16 app = Flask(__name__)
17 app.config.update(DEBUG=True)
18
19 @app.route('/asyn/', methods=['GET'])
20 def test_asyn_one():
21     print("asyn has a request!")
22     #print(urllib.request.urlopen('http://127.0.0.1:8000/async1'))
23     os.system("ping -c 5 127.0.0.1")
24     return 'hello asyn'
25
26
27 if __name__ == "__main__":
28     # app.run()
29     http_server = WSGIServer(('', 5000), app)
30     http_server.serve_forever()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: