tornadod的异步代码
2016-12-09 15:08
120 查看
#!/usr/bin/env python # -*- coding: utf-8 -*- import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import tornado.httpclient import tornado.gen from tornado.concurrent import run_on_executor # 这个并发库在python3.2版本以后自带, 在python2需要安装 sudo pip install futures from concurrent.futures import ThreadPoolExecutor import time class SleepHandler(tornado.web.RequestHandler): executor = ThreadPoolExecutor(2) @tornado.web.asynchronous @tornado.gen.coroutine def get(self): """ 若是要实现异步并且该请求需要等待执行结果则加入yield 否则可以将yield去掉,程序会继续往下执行 """ res = yield self.sleep() self.write("when i sleep %s s bbb" % res) self.finish() #@tornado.web.asynchonous装饰器时,Tornado永远不会自己关闭连接,需要显式的self.finish()关闭 @run_on_executor def sleep(self): time.sleep(6) return 6 class NormalHandler(tornado.web.RequestHandler): def get(self): self.write("normal handler") if __name__ == "__main__": app = tornado.web.Application(handlers=[ (r"/sleep", SleepHandler), (r"/normal", NormalHandler), ]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(8888) tornado.ioloop.IOLoop.instance().start()
详情请看:http://www.tuicool.com/articles/36ZzA3
http://www.tuicool.com/articles/Ir2uai
相关文章推荐
- Python Web框架Tornado的异步处理代码示例
- Python Web框架Tornado的异步处理代码演示样例
- C# 代码示例详尽剖析异步调用的两种方法
- 在你的服务器端代码中使用线程和创建异步处理
- C# 下面的代码示例演示如何结束异步操作以获取请求的流,然后开始一个请求以获取响应。....
- 写个 JavaScript 异步调用框架 (Part 3 - 代码实现)
- 比较简单的异步加载JS文件的代码
- 【转载】c#(Socket)异步套接字代码示例
- C#完整的通信代码(点对点,点对多,同步,异步,UDP,TCP)
- c#(Socket)异步套接字代码
- MiniGUI的vxworks环境移植(2): cygwin+tornado交叉编译minigui库和示例代码
- XMLHTTP异步获取返回值的代码超时测试(AJAX)
- ASP.NET 2.0 中的新特性: 异步页功能(示例代码下载)
- MSDN官方的ASP.Net异步页面的经典示例代码
- 一个利用了异步处理,事件驱动,对象序列化等技术的类Parser完整代码示例
- ASP.NET 2.0 中的新特性: 异步页功能(示例代码下载)
- ASP.NET 2.0 中的新特性: 异步页功能(示例代码下载)
- 如何对远程对象进行异步调用(示例代码)
- 下面的代码示例使用 BeginGetRequestStream 方法对流实例发出异步请求。
- C# winform进度条(异步) 窗体设计代码