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. Scrapy爬虫框架下执行爬虫的方法

    在使用Scrapy框架进行爬虫时,执行爬虫文件的方法是 scrapy crawl xxx ,其中 xxx 是爬虫文件名. 但是,当我们在建立了多个文件时,使用上面的命令时会比较繁琐麻烦,我们就可以使用 ...

  2. centos6最小化安装默认没有 NetworkManager服务

    转载Centos6最小化安装中设置网卡默认启动   Centos 6.0版本提供了一个"最小化"(Minimal)安装的选项.这是一个非常好的改进,因为系统中再也不会存在那些不必要 ...

  3. linux中的udev(unix devices)

    最开始的时候,linux预先定义了很多种设备文件,(不管这种设备是否存在), 在/dev/下 但是即使这些设备文件不存在, 这样/dev下的文件就会很多, 而且像upan在插拔顺序不同, 所对应的映射 ...

  4. 架构-数据库访问-SQL语言进行连接数据库服务器-OLE:OLE

    ylbtech-架构-数据库访问-SQL语言进行连接数据库服务器-OLE:OLE Object Linking and Embedding,对象连接与嵌入,简称OLE技术.OLE 不仅是桌面应用程序集 ...

  5. DRF的路由生成类的使用

    DRF路由生成类的使用 对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework ...

  6. java实现多种加密模式的AES算法-总有一种你用的着

    https://blog.csdn.net/u013871100/article/details/80100992 AES-128位-无向量-ECB/PKCS7Padding package com. ...

  7. 抓包工具tcpdump用法说明--2

    第一招: 通俗的说,tcpdump是一个抓包工具,用于抓取互联网上传输的数据包.形象的说,tcpdump就好比是国家海关,驻扎在出入境的咽喉要道,凡是要入境和出境的集装箱,海关人员总要打开箱子,看看里 ...

  8. 15年6月8号 jsp内置对象总结

    jsp的内置对象:主要有三个request.session.application:而且三者之间有各自不同的特点,在不同的情况下,使用不同的对象会有不同的效果, 其中: 1.request(特点):一 ...

  9. 洛谷 P1462 通往奥格瑞玛的道路(二分答案,堆优化dijkstra)

    传送门 解题思路 首先看题目问题,求经过的所有城市中最多的一次收取的费用的最小值是多少.一看“最大值最小”就想到了二分答案. 在读一遍题目,就是二分收取的费用,然后对于每一个二分的费用,跑一边最短路, ...

  10. day17-django基础

    3. Django框架 版本:1.11 创建: django-admin startprojcet xxx cd xxx python manage.py startapp app01 python ...