1.gen.coroutine的作用

自动执行生成器

2.Future对象

在介绍异步使用之前,先了解一下Future对象的作用。

Future简单可以理解为一个占位符,将来会执行的对象,类似javascript中的promise对象,是实现异步的关键。

class Future(object):
def __init__(self):
self._callback = []
self._result = None
self._done = False def set_callback(self, cb):
self._callback.append(cb) def _run_callback(self):
for cb in self._callback:
cb() def set_result(self, result)
self._done = True
self._result = result
self._run_callback() def is_ready(self):
return self._done is True

_result:返回结果值

_done:是否完成

_callback:完成后,执行的回调列表

set_result():赋值result,Future对象完成,执行回调。

3.callback实现异步

from tornado.httpclient import AsyncHTTPClient
from tornado.web import RequestHandler class Test1Handler(RequestHandler):
def get(self, *args, **kwargs):
http_client = AsyncHTTPClient()
http_client.fetch('www.baidu.com', callback = self.on_fetched)
print('done') def on_fetched(self, response):
print('response')

运行结果:

源码分析:

def fetch(self, request, callback=None, raise_error=True, **kwargs):
if callback is not None:
def handle_future(future):
response = future.result()
self.io_loop.add_callback(callback, response)
future.add_done_callback(handle_future)
def handle_response(response):
if raise_error and response.error:
future.set_exception(response.error)
else:
future.set_result(response)
self.fetch_impl(request, handle_response)
return future def fetch_impl(self, request, callback):
raise NotImplementedError()

fetch函数返回一个Future类型对象,fetch_impl()执行完毕,返回结果response作为参数,执行回调handle_response

handle_response将response赋值给future。future状态变为已完成,执行future的callback函数handle_future,handle_future将callback加入ioloop执行队列,response作为参数。

由ioloop调度完成callback。

关键点在于,Future占位符控制了什么时候执行回调。

3.gen.coroutine实现异步

from tornado.httpclient import AsyncHTTPClient
from tornado.web import RequestHandler
from tornado import gen class Test1Handler(RequestHandler):
@gen.coroutine
def get(self, *args, **kwargs):
http_client = AsyncHTTPClient()
response = yield http_client.fetch('http://www.baidu.com')
print('response')

运行结果:

当执行到yield 表达式时,表达式会返回一个Future占位符,然后返回,当表达式执行完毕后,自动继续执行生成器。

关键点在于,gen.coroutine使生成器可以自动执行。

源码分析:

def coroutine(func, replace_callback=True):
return _make_coroutine_wrapper(func, replace_callback=True)
def _make_coroutine_wrapper(func, replace_callback):
try:
yielded = next(result)
except (StopIteration, Return) as e:
future.set_result(getattr(e, 'value', None))
except Exception:
future.set_exc_info(sys.exc_info())
else:
Runner(result, future, yielded)

result:生成器对象

yielded:Future对象,生成器首次执行结果,如果异常StopIteration,表示生成器执行完毕,将结果设置成future的值,返回,装饰器gen.coroutine返回的为Future对象。

Runner:判断yielded是否完成,完成则执行run函数,继续执行生成器;否则,添加run函数到这个Future对象yielded,执行完毕之后,才调用run函数。

class Runner(object):
def __init__(self, gen, result_future, first_yielded):
if self.handle_yield(first_yielded):
gen = result_future = first_yielded = None
self.run() def handle_yield(self, yielded):
self.future = convert_yielded(yielded) if not self.future.done() or self.future is moment:
self.io_loop.add_future(
self.future, lambda f: self.run())
return False
return True def run(self):
if self.running or self.finished:
return
try:
self.running = True
while True:
future = self.future
if not future.done(): #执行run时generator返回的那个future必须已经有结果,否则就没必要传回到generator中了
return
self.future = None
try:
value = future.result()
yielded = self.gen.send(value)
except (StopIteration, Return) as e:
#generator执行完毕并成功的处理
except Exception:
#generator执行过程中异常的处理
if not self.handle_yield(yielded):
return
finally:
self.running = False

handle_yield:判断Future对象yielded是否完成,未完成,注册run()函数回调到这个Future对象,完成,才调用。

run:将yielded这个Future对象的result,作为参数传递给生成器,继续执行生成器。

Tornado中gen.coroutine详解的更多相关文章

  1. Unity3D中的Coroutine详解

    Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这样的代码. 疑问: yield是什么? Coroutine是什么? unity的coroutin ...

  2. 【Unity3D/C#】Unity3D中的Coroutine详解

    Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这样的代码. 疑问: yield是什么? Coroutine是什么? unity的coroutin ...

  3. php中关于引用(&)详解

    php中关于引用(&)详解 php的引用(就是在变量或者函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的变量名访问同一个变量内容. 与C语言中的指针是有差别的.C语言中的 ...

  4. JavaScript正则表达式详解(二)JavaScript中正则表达式函数详解

    二.JavaScript中正则表达式函数详解(exec, test, match, replace, search, split) 1.使用正则表达式的方法去匹配查找字符串 1.1. exec方法详解 ...

  5. AngularJS select中ngOptions用法详解

    AngularJS select中ngOptions用法详解   一.用法 ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上. 数组: label for value in a ...

  6. 【转载】C/C++中extern关键字详解

    1 基本解释:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.此外extern也可用来进行链接指定. 也就是说extern ...

  7. oracle中imp命令详解 .

    转自http://www.cnblogs.com/songdavid/articles/2435439.html oracle中imp命令详解 Oracle的导入实用程序(Import utility ...

  8. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  9. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

随机推荐

  1. 转:CMake快速入门教程-实战

    CMake快速入门教程:实战 收藏人:londonKu     2012-05-07 | 阅:10128  转:34    |   来源   |  分享               0. 前言一个多月 ...

  2. Condition Variables

    Condition Variables Condition variables are synchronization primitives that enable threads to wait u ...

  3. Java中用HttpsURLConnection访问Https链接

    在web应用交互过程中,有很多场景需要保证通信数据的安全:在前面也有好多篇文章介绍了在Web Service调用过程中用WS-Security来保证接口交互过程的安全性,值得注意的是,该种方式基于的传 ...

  4. commit your changes or stash them before you can merge

    今天用git pull来更新代码,遇到了下面的问题: 今天git pull 出现以下问题 Please commit your changes or stash them before you mer ...

  5. RHEL/CentOS 7.x/6.x/5.x开启EPEL仓库

    说明 原文链接 翻译:@adolphlwq 项目地址 这篇指南文章教你如何在 RHEL/CentOS 7.x/6.x/5.x 系统中开启EPEL仓库支持,以便你可以使用 yum 命令 安装额外的标准开 ...

  6. 【Struts2】Struts2框架的搭建

    1,Struts2简介 struts1和struts2都是由Apache组织发布的,但是比较有趣的是struts2和struts1并没有“血缘关系”.在Apache发布struts1之后,当时是还是非 ...

  7. Spring Cloud问题分析

    基于Spring Cloud框架开发时,经常会碰到各种开发问题,那么碰到这些问题时如何去解决呢?下面描述基于Spring Cloud问题定位的基本思路,大概可以分为如下几步: 排查配置问题 环境问题 ...

  8. MySQL USING 和 HAVING 用法

    USING 用于表连接时给定连接条件(可以理解为简写形式),如 SELECT * FROM table1 JOIN table2 ON table1.id = table2.id   使用 USING ...

  9. list、map、数组 转换

    list,set,map,数组间的相互转换1.list转setSet set = new HashSet(new ArrayList()); 2.set转listList list = new Arr ...

  10. Easyui 中 Tabsr的常用方法

    注:index必须为变量 tab页从0开始 //新增一个tab页var index = 0;$('#tt').tabs('add',{ title: 'Tab'+index, content: '&l ...