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的更多相关文章

  1. Tornado @tornado.gen.coroutine 与 yield

    在使用 Tornado 的过程中产生了以下疑问: 什么时候需要给函数增加 @tornado.gen.coroutine 什么时候调用函数需要 yield @tornado.gen.coroutine ...

  2. Tornado源码分析系列之一: 化异步为'同步'的Future和gen.coroutine

    转自:http://blog.nathon.wang/2015/06/24/tornado-source-insight-01-gen/ 用Tornado也有一段时间,Tornado的文档还是比较匮乏 ...

  3. 使用tornado的gen.coroutine进行异步编程

    在tornado3发布之后,强化了coroutine的概念,在异步编程中,替代了原来的gen.engine, 变成现在的gen.coroutine.这个装饰器本来就是为了简化在tornado中的异步编 ...

  4. Tornado中gen.coroutine详解

    1.gen.coroutine的作用 自动执行生成器 2.Future对象 在介绍异步使用之前,先了解一下Future对象的作用. Future简单可以理解为一个占位符,将来会执行的对象,类似java ...

  5. 如何捕捉@tornado.gen.coroutine里的异常

    from tornado import gen from tornado.ioloop import IOLoop @gen.coroutine def throw(a,b): try: a/b ra ...

  6. python yield用法 (tornado, coroutine)

    yield关键字用来定义生成器(Generator),其具体功能是可以当return使用,从函数里返回一个值,不同之处是用yield返回之后,可以让函数从上回yield返回的地点继续执行.也就是说,y ...

  7. 深入tornado中的http1connection

    前言 tornado中http1connection文件的作用极其重要,他实现了http1.x协议. 本模块基于gen模块和iostream模块实现异步的处理请求或者响应. 阅读本文需要一些基础的ht ...

  8. 学习 Tornado

    异步编程 预习 lambda Lambda functions can be used wherever function objects are required. They are syntact ...

  9. ngx_lua_API 指令详解(五)coroutine.create,coroutine.resume,coroutine.yield 等集合指令介绍

    ngx_lua 模块(原理实现) 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM: 2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问: ...

随机推荐

  1. onchange and oninput

    https://www.w3schools.com/jsref/event_oninput.asp Supported HTML tags: <input type="color&qu ...

  2. ORA-20011

    Sun Jul 23 22:09:07 2017DBMS_STATS: GATHER_STATS_JOB encountered errors. Check the trace file.Errors ...

  3. oracle--高级使用(merge)(递归START WITH)分析函数over

    1.俩种表复制语句 SELECT INTO和INSERT INTO SELECT两种表复制语句 CT: create table <new table> as select * from ...

  4. [2019杭电多校第七场][hdu6656]Kejin Player

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意为从i级花费a元有p的概率升到i+1级,有1-p的概率降到x级(x<i),查询从L级升 ...

  5. python抓取往期双色球

    import requests import json,csv ''' http://m.zhcw.com/clienth5.do?lottery=FC_SSQ&pageSize=20& ...

  6. 问题 M: 最亲密的x个人

    问题 M: 最亲密的x个人 时间限制: 1 Sec  内存限制: 128 MB提交: 412  解决: 38[提交] [状态] [命题人:jsu_admin] 题目描述 有一天,地球受到了降维打击,从 ...

  7. 一台电脑关联多个git账号

    一台电脑连接多个git账号 现需要一台电脑连接gitlab,github,码云,之前的操作时,用公司账号,在这几个地方都注册一遍,导致自己就有两类号,一个自己的,一个公司的,这样也是可以,但总是不太好 ...

  8. js数组中的引用类型

    我们看一下这个例子: let a={tile:'深复制'}; let b=a; a.title='浅复制'; 那么我们会获得两个对象,一个a,一个b,a的title是浅复制,b的title是深复制.但 ...

  9. nginx读取请求体

    请求体的读取一般发生在nginx的content handler中,一些nginx内置的模块,比如proxy模块,fastcgi模块,uwsgi模块等,这些模块的行为必须将客户端过来的请求体(如果有的 ...

  10. WannaCry的UWP版,哈哈哈