Tornado是一款非阻塞web server,今天做了一个很简单的尝试
non-blocking I/O, 我简单的理解是“允许某一个操作可以继续进行,而不必等待某一资源的响应,预提供一个回调函数,用于处理、响应该资源的结果(当该资源返回相关内容的时候)”
对比异步I/O,我们最常见的就是同步I/O(线性编程),一次请求访问另一个资源,必须等待该资源的成功返回,方可进行下一步操作,如果该资源无响应(或异常),程序就终止(受限)于此。
更多异步I/O,可以参考:
http://en.wikipedia.org/wiki/Asynchronous_IO
这里我个人很浅显地描述下sync/async的大致对比:
下面就是我的测试代码:
async_sync_test_invoke3.py,一个普通的web app,将会被用于异步请求的调用
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
'''
The below is only a testing which will be exected to kill time,
and then we can find the asynchronous effect from the front page.
'''
for i in range(1, 100000):
print "kill time"
self.write("hello")
settings = {
#"debug": True,
}
application = tornado.web.Application([
(r"/async-sync-test/", MainHandler),
], **settings)
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8084)
tornado.ioloop.IOLoop.instance().start()
async_test3.py, 将会提供一次异步请求调用async_sync_test_invoke3.py提供的/async-sync-test/
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.httpclient import AsyncHTTPClient
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
http = tornado.httpclient.AsyncHTTPClient()
http.fetch("http://localhost:8084/async-sync-test/", callback=self._test_callback)
self.write("Hello to the Tornado world! ")
'''
Flushes the current output buffer to the network.
'''
self.flush()
'''
_test_callback is a callback function used for the processing of the response from the async request
'''
def _test_callback(self, response):
self.write(response.body)
'''
refer the offical document, we are responsible for the invocation of the finish function in the async case.
'''
self.finish()
settings = {
#"debug": True,
}
application = tornado.web.Application([
(r"/", MainHandler),
], **settings)
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8083)
tornado.ioloop.IOLoop.instance().start()
分别启动async_sync_test_invoke3.py , async_test3.py
我们访问http://localhost:8083/的时候,可以在页面上看到较为明显的异步效果,“hello”的内容是在稍后的response中获得的。
小结:
如果是sync I/O,我们只启动一个类似sync_test代码(此处并没有提供),同样访问http://localhost:8083/程序将会整个被http error提醒,因为这里无法成功连接预期http resource,程序的线性表现在此看见。
如果是async I/O,我们只启动async_test3.py,从页面角度的看至少能获得“Hello to the Tornado world!”内容,丢失的是异步部分的内容,当然后台会有错误输出。
通过合理使用sync/async I/O,可以给web应用不错的性能,以及带来不错的用户体验
请大家帮忙指正及建议,如果有理解错误的地方!
- 大小: 55.2 KB
分享到:
相关推荐
NULL 博文链接:https://jxee.iteye.com/blog/1608820
跨平台 Python 异步聊天机器人框架 - Asynchronous multi-platf-nonebot2
A-Designer-s-Guide-to-Asynchronous-VLSI
xapp523-lvds-4x-asynchronous-oversampling
徐聪-Asynchronous Federated Optimization1
swift开发、iOS、macOS、iphone、后台、第2版本
异步时钟设计,解决亚稳态,异步FIFO读写等。
基于Verilog设计的异步FIFO,有效防止速率不匹配造成的数据丢失,文件中包含源码以及testbench,且有注释,可读性强
可实现异步调用子程序,跨线程访问控件,实现同步调用
The Task-based Asynchronous Pattern (TAP) is a new pattern for asynchrony in the .NET Framework. It is based on the Task and Task<TResult> types in the System.Threading.Tasks namespace, which are used...
TA-JS-asynchronous-js-TJaaao
将异步电机等效为直流电机,即将定转子解耦的同步旋转模型
026-asynchronous-native-java-calls-spring
[TakeLionOS汇编与C语言自学笔记-第五课] 版权所有 2021年 江志剑 按照《21天学通C语言》(第七版),有如下二个例子。 一个例子输出一段话,另一个例子计算年份: //1、files:hello1.c hello1no.s hello1.exe #...
[TakeLionOS汇编与C语言自学笔记-第三课] 版权所有 2021年 江志剑 按照《21天学通C语言》(第七版),有如下二个修改版的例子。其中一个改为输出黑桃符号,其中一个改为输出红心符号,是练习题6修改一下的: //1、...
[TakeLionOS汇编与C语言自学笔记-第四课] 版权所有 2021年 江志剑 按照《21天学通C语言》(第七版),有如下二个例子。其中一个改为输出黑桃符号,是练习题3修改一下的: //1、files:helloxy.c helloxy.i ...
[TakeLionOS汇编与C语言自学笔记-第六课] 版权所有 2021年 江志剑 按照《21天学通C语言》(第七版),有如下一个例子P17。 将整个程序的代码,包括行号显示出来: //1、files:hellolistit.c hellolistitno.s ...
所以启用了GCC的编译选项-fno-asynchronous-unwind-tables,直接预处理为cfi宏的汇编指令。 这里就是包含*no.s形式的文件。 比较发现,第一个文件和第二个文件,其余基本一样,但是第12行开始的调用函数完全不一样...
Pika是AMQP 0-9-1协议的纯Python实现,包括RabbitMQ的扩展。新增功能接受用于处理消息的回调。 原始邮件将消息写入日志文件。 处理并记录连接到发布者时的错误。 记录读取的消息数和处理失败的消息数。 添加班级...
The Task-based Asynchronous Pattern