tornado的异步效果
第一种方式: import tornado.ioloop
import tornado.web
from tornado import gen
from tornado.concurrent import Future
import time #########异步效果1,iploop
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine##加一个装饰器
def get(self):
import time
###等待五秒种
future=Future()
self.write('hello,world ')
##五秒钟的超时时间,
tornado.ioloop.IOLoop.current().add_timeout(time.time()+,self.doing)##等待五秒的时间
yield future
def doing(self,*args,**kwargs):
self.write('yibu ')
self.finish() class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write('index') ##下面是路由映射
appliaction=tornado.web.Application([
(r'/main',MainHandler),
(r'/index', IndexHandler),
])
第二种方式: import tornado.ioloop
import tornado.web
from tornado import gen
from tornado.concurrent import Future
import time ######异步效果2,AsyncHTTPClient
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine##加一个装饰器
def get(self):
import time
from tornado import httpclient
http=httpclient.AsyncHTTPClient()
yield http.fetch('http://www.geogle.com',self.doing ) def doing(self,*args,**kwargs):
self.write('yibu ')
self.finish() class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write('index') ##下面是路由映射
appliaction=tornado.web.Application([
(r'/main',MainHandler),
(r'/index', IndexHandler),
]) ##settings配置
if __name__ == '__main__':
appliaction.listen()
tornado.ioloop.IOLoop.instance().start()
第三种方式: import tornado.ioloop
import tornado.web
from tornado import gen
from tornado.concurrent import Future
import time #异步效果3,future
future=None
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine##加一个装饰器
def get(self):
import time
global future
future=Future()
future.add_done_callback(self.doing)#####在这里设置返回值,future,当future里面的result值发生改变的时候就会触发这个的执行
yield future def doing(self,*args,**kwargs):
self.write('yibu ')
self.finish() class IndexHandler(tornado.web.RequestHandler):
def get(self):
global future
future.set_result(None)##设置返回值
self.write('index') ##下面是路由映射
appliaction=tornado.web.Application([
(r'/main',MainHandler),
(r'/index', IndexHandler),
]) ##settings配置
if __name__ == '__main__':
appliaction.listen()
tornado.ioloop.IOLoop.instance().start()
tornado的异步效果的更多相关文章
- 深入理解Tornado——一个异步web服务器
本人的第一次翻译,转载请注明出处:http://www.cnblogs.com/yiwenshengmei/archive/2011/06/08/understanding_tornado.html原 ...
- 在 tornado 中异步无阻塞的执行耗时任务
在 tornado 中异步无阻塞的执行耗时任务 在 linux 上 tornado 是基于 epoll 的事件驱动框架,在网络事件上是无阻塞的.但是因为 tornado 自身是单线程的,所以如果我们在 ...
- Tornado的异步非阻塞
阻塞和非阻塞Web框架 只有Tornado和Node.js是异步非阻塞的,其他所有的web框架都是阻塞式的. Tornado阻塞和非阻塞两种模式都支持. 阻塞式: 代表:Django.Flask.To ...
- 深入理解yield(三):yield与基于Tornado的异步回调
转自:http://beginman.cn/python/2015/04/06/yield-via-Tornado/ 作者:BeginMan 版权声明:本文版权归作者所有,欢迎转载,但未经作者同意必须 ...
- tornado 之 异步非阻塞
异步非阻塞 1.基本使用 装饰器 + Future 从而实现Tornado的异步非阻塞 import tornado.web import tornado.ioloop from tornado im ...
- tornado 11 异步编程
tornado 11 异步编程 一.同步与异步 同步 #含义:指两个或两个以上随时间变化的量在变化过程中保持一定的相对关系 #现象:有一个共同的时钟,按来的顺序一个一个处理 #直观感受:需要等待,效率 ...
- Python核心框架tornado的异步协程的2种方式
什么是异步? 含义 :双方不需要共同的时钟,也就是接收方不知道发送方什么时候发送,所以在发送的信息中就要有提示接收方开始接收的信息,如开始位,同时在结束时有停止位 现象:没有共同的时钟,不考虑顺序来了 ...
- Tornado中异步框架的使用
tornado的同步框架与其他web框架相同都是处理先来的请求,如果先来的请求阻塞,那么后面的请求也会处理不了.一直处于等待过程中.但是请求一旦得到响应,那么: 请求发送过来后,将需要的本站资源直接返 ...
- Tornado之异步非阻塞
同步模式:同步模式下,只有处理完前一个任务下一个才会执行 class MainHandler(tornado.web.RequestHandler): def get(self): time.slee ...
随机推荐
- Your accoutn already has a valid IOS Distribution certificate
这个问题是IOS证书不对,登录Apple开发中心,清空所有证书,然后再Archive->Reset.
- mesg命令帮助文档(ubuntu 18.04)
MESG() User Commands MESG() NAME mesg - display (or do not display) messages from other users SYNOPS ...
- springboot06-swagger2 自动化api文档
1.springboot 项目中添加swagger2依赖: <dependency> <groupId>org.springframework.boot</groupId ...
- for-each 格式
public class D21LX { public static void main(String arge[]){ fish [] a1=new fish[3]; a1[0] = new fis ...
- Git命令用于检查特定提交的差异
假设你的提交日志如下(可以通过 git log 命名查看) commit 14af3315a2b1234daac74ece61ef913007043e06 Author: wuxianqiang &l ...
- 在IDEA中新建Spring Boot项目
新建项目 选择项目构建方式 选择项目依赖 新建项目成功后(Maven构建方式)
- CSL 的字符串(单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/551/D 题目大意: 题目描述 CSL 以前不会字符串算法,经过一年的训练,他还是不会……于是他打算向你求助. 给定 ...
- D. Time to go back(思维)
题目链接:http://codeforces.com/gym/100952/problem/D 题目大意:n个礼物,m个人,要给m个人中的k个人买大于等于d的礼物,其他人随意,问你选择礼物的方案数(不 ...
- zabbix系列 ~ linux监控相关
Linux 监控 一 相关名词解释 cpu context switch (上下文切换) 1 定义 CPU给每个任务一定的服务时间,当时间片轮转的时候,需要把当前状态保存下来,同时加载下一个 ...
- stega -- Pcat老入群题
stega -- Pcat老入群题 Pcat师傅的题果然给力,就是看着wp也是琢磨了半天. WP地址:http://mp.weixin.qq.com/s/T9jJLACiZNB6FR226IjmEA ...