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代码直接访问: ...
随机推荐
- sqlserver 查询当前阻塞进程 并杀掉
select * from master.dbo.sysprocesses where DB_NAME(dbid)=’test’ and spid<>@@SPID 看看阻塞的进程 然后ki ...
- 数据库-SqlServer 行转列,列转行
两篇行转列,列转行的实例文章: 第1篇:https://www.cnblogs.com/cpcpc/archive/2013/04/08/3009021.html 第2篇:https://mp.wei ...
- WPF 实现多语言支持
WPF 多语言有各种实现方式.如 https://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files,后来发现这个 ...
- Openstack 通过 SQLAlchemy-ORM 访问数据库
目录 目录 Demo SQLAlchemy 数据库的初始化 数据库的操作实现 数据库的操作请求 全部查询 单个查询 创建 更新 删除 Demo Github/JmilkFan/my-code-repe ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_9_字节输入流读取字节数据
硬盘读取到内存 read值会读一个字节 a.txt里面a变成整数就是97 读取到末尾,返回-1 再读一次还是-1 读取的代码是重复的.可以使用循环去读取.while循环. 转行成char类型的 ...
- webpack打包html里的img图片
对待css里的图片, 因为已经通过引入css文件到js,打包了,可以正常通过module.rules.test检测到,然后正常打包. 但是对于html里的图片, 这个需要安装一个插件html-with ...
- node基本介绍及使用
1.什么是node 简单的说node.js就是运行在服务端的JavaScript 官网地址:http://nodejs.cn/ 2.node安装 2.1下载node node下载:https://no ...
- Codeforces 609E (Kruskal求最小生成树+树上倍增求LCA)
题面 传送门 题目大意: 给定一个无向连通带权图G,对于每条边(u,v,w)" role="presentation" style="position: rel ...
- POJ 1905 题解(二分+几何)
题面 传送门 分析 如图:已知AB=L,弧AB=L(1+nC)" role="presentation" style="position: relative;& ...
- Spring事务嵌套引发的问题--Transaction rolled back because it has been marked as rollback-only
转载https://blog.csdn.net/f641385712/article/details/80445912 读了两边才找到问题