yield与gen.coroutine
def d():
for i in range(2):
yield i def b():
yield d()
print("b")
yield "bb" def a():
g_b = b()
print(g_b)
g_d = next(g_b)
print(g_d)
v_d = next(g_d)
print(v_d) #
v_d = next(g_d)
print(v_d) #
b_next = next(g_b)
print(b_next) # bb if __name__ == "__main__":
a()
#---输出
# <generator object b at 0x00000000033A3E60>
# <generator object d at 0x000000000389F150>
#
#
# b
# bb ----------------------
def d():
for i in range(2):
yield i def b():
yield d()
print("b") def a():
g_b = b()
print(g_b) # <generator object b at 0x00000000033A3E60>
g_d = next(g_b)
print(g_d) # <generator object d at 0x000000000389F150>
v_d = next(g_d)
print(v_d) #
v_d = next(g_d)
print(v_d) #
b_next = next(g_b)
print(b_next)
#---输出
<generator object b at 0x00000000033A3E60>
<generator object d at 0x000000000349F150>
0
1
b
Traceback (most recent call last):
File "E:\eclipse-workspace\demo_python\demo\yield_demo.py", line 49, in <module>
a()
File "E:\eclipse-workspace\demo_python\demo\yield_demo.py", line 19, in a
b_next = next(g_b)
StopIteration
---------------------- def d():
for i in range(2):
yield i def b():
yield d()
print("b")
yield def a():
g_b = b()
print(g_b) # <generator object b at 0x00000000033A3E60>
g_d = next(g_b)
print(g_d) # <generator object d at 0x000000000389F150>
v_d = next(g_d)
print(v_d) #
v_d = next(g_d)
print(v_d) #
b_next = next(g_b)
print(b_next)
#---输出
<generator object b at 0x00000000033A3E60>
<generator object d at 0x000000000349F150>
0
1
b
None --------------
@gen.coroutine
def gen_d():
for i in range(10):
raise gen.Return(i) @gen.coroutine
def gen_b():
v = yield gen_d()
print("b") @gen.coroutine
def gen_a():
var = yield gen_b()
print(var) if __name__ == "__main__":
ioloop.IOLoop.current().run_sync(gen_a)
--输出
b
None
-----------------------------
说明:方法gen_d的注解gen.coroutine去掉之后,
gen_d的返回值不再是Future类型,gen_b方法在执行以一条语句之后,
不会继续执行后面的语句
#@gen.coroutine
def gen_d():
for i in range(10):
raise gen.Return(i) @gen.coroutine
def gen_b():
v = yield gen_d()
print("b") @gen.coroutine
def gen_a():
var = yield gen_b()
print(var) if __name__ == "__main__":
ioloop.IOLoop.current().run_sync(gen_a)
--输出
0
-----------------------------
yield直接返回
@gen.coroutine
def gen_d():
for i in range(10):
raise gen.Return(i) @gen.coroutine
def gen_b():
yield gen_d() @gen.coroutine
def gen_a():
var = yield gen_b()
print(var) if __name__ == "__main__":
ioloop.IOLoop.current().run_sync(gen_a)
---
输出:None
----------------------
raise返回:
@gen.coroutine
def gen_d():
for i in range(10):
raise gen.Return(i) @gen.coroutine
def gen_b():
raise gen.Return(gen_d()) @gen.coroutine
def gen_a():
var = yield gen_b()
print(var) if __name__ == "__main__":
ioloop.IOLoop.current().run_sync(gen_a)
输出:<Future finished result=0>
--------------------------------
有异步的情况下不能直接返回:
@gen.coroutine
def gen_d():
url = "http://x.x.x.x:8060/api/v1/health"
result = yield AsyncHTTPClient(defaults=dict(request_timeout=10)).fetch(url, method="GET")
raise gen.Return((result.code, result.body)) @gen.coroutine
def gen_b():
raise gen.Return((yield gen_d())) @gen.coroutine
def gen_a():
var = yield gen_b()
print(var) if __name__ == "__main__":
ioloop.IOLoop.current().run_sync(gen_a)
输出:(200, b'ok')
yield与gen.coroutine的更多相关文章
- Tornado @tornado.gen.coroutine 与 yield
在使用 Tornado 的过程中产生了以下疑问: 什么时候需要给函数增加 @tornado.gen.coroutine 什么时候调用函数需要 yield @tornado.gen.coroutine ...
- Tornado源码分析系列之一: 化异步为'同步'的Future和gen.coroutine
转自:http://blog.nathon.wang/2015/06/24/tornado-source-insight-01-gen/ 用Tornado也有一段时间,Tornado的文档还是比较匮乏 ...
- 使用tornado的gen.coroutine进行异步编程
在tornado3发布之后,强化了coroutine的概念,在异步编程中,替代了原来的gen.engine, 变成现在的gen.coroutine.这个装饰器本来就是为了简化在tornado中的异步编 ...
- Tornado中gen.coroutine详解
1.gen.coroutine的作用 自动执行生成器 2.Future对象 在介绍异步使用之前,先了解一下Future对象的作用. Future简单可以理解为一个占位符,将来会执行的对象,类似java ...
- 如何捕捉@tornado.gen.coroutine里的异常
from tornado import gen from tornado.ioloop import IOLoop @gen.coroutine def throw(a,b): try: a/b ra ...
- python yield用法 (tornado, coroutine)
yield关键字用来定义生成器(Generator),其具体功能是可以当return使用,从函数里返回一个值,不同之处是用yield返回之后,可以让函数从上回yield返回的地点继续执行.也就是说,y ...
- 深入tornado中的http1connection
前言 tornado中http1connection文件的作用极其重要,他实现了http1.x协议. 本模块基于gen模块和iostream模块实现异步的处理请求或者响应. 阅读本文需要一些基础的ht ...
- 学习 Tornado
异步编程 预习 lambda Lambda functions can be used wherever function objects are required. They are syntact ...
- ngx_lua_API 指令详解(五)coroutine.create,coroutine.resume,coroutine.yield 等集合指令介绍
ngx_lua 模块(原理实现) 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM: 2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问: ...
随机推荐
- Linux动态库和静态库
Linux下动态库查看办法:nm -D libavformat.so Linux下静态库查看办法:ar -t libavformat.a ------------------------------- ...
- OGG-DDL复制
http://blog.sina.com.cn/s/blog_96d348df0102vg6q.html OGG目前只支持Oracle和TeraData的ddl复制,Oracle数据库能够支持除去数据 ...
- Markdown Memo(memorandum)
居中 html语法 <center>居中</center> 左对齐 <p align="left">左对齐</p> 右对齐 < ...
- spring切换环境变量——@Profile注解的使用
在容器中如果存在同一类型的多个组件,也可以使用@Profile注解标识要获取的是哪一个bean,这在不同的环境使用不同的变量的情景特别有用.例如,开发环境.测试环境.生产环境使用不同的数据源,在不改变 ...
- 解决Delphi窗体缩放の疑难杂症
http://anony3721.blog.163.com/blog/static/511974201082235754423/ 解决Delphi窗体缩放の疑难杂症 2010-09-22 15:57: ...
- 【ABAP系列】SAP ABAP WS_DELIVERY_UPDATE 修改数量、过账日期并发货过账
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP WS_DELI ...
- JPA 学习笔记
eclipse 新建jpa项目 : 修改 persistence.xml 文件 创建 Customer 类: column 名称和数据库名称对应则不用写 类写好后在 persistence.xm ...
- mooc-IDEA 高效定位代码--004
十.IntelliJ IDEA -高效定位代码-精准搜索 1.快速定位类:Navigate->Class... [Ctrl+N] 2.文件:Navigate->File.. [Ct ...
- Java提取文本文档中的所有网址(小案例介绍正则基础知识)
正则表达式基础以及Java中使用正则查找 定义: 正则表达式是一些用来匹配和处理文本的字符串 正则的基础(先大致了解下) 1. 正则表达式的作用 查找特定的信息(搜索) 替换一些文本(替换) 2. 正 ...
- 创建Maven项目时,出现系列的错误提示的修改方法
1.创建Maven项目成功之后,需要修改一些配置, (1).java版本改为“本系统中java的版本号” 问题一:(2).Dynamic Web Module的version要改为2.5以上,然而本人 ...