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代码直接访问: ...
随机推荐
- 500 cannot be cast to javax.xml.registry.infomodel
在使用mybatis的时候每次一调用一个返回User类型的sql时,总是会报错如下: com.xx.all.domain.User cannot be cast to javax.xml.regist ...
- jmeter的日常特殊参数化
1.map转译符号: 如果///Mobile///:///18888888888/// 需要再参数化请这样做,////Mobile////://///${Mobile}///// 2.in ...
- centos php 安装 decrypt
CentOS php Fatal error: Call to undefined function mcrypt_decrypt() // yum安装没有 #yum install libmcryp ...
- C++编译-链接错误集合
1,无法解析的外部符号,链接错误,原因:没找到某个符号(变量或函数)的定义体,一般是对应函数没实现,或第三方库没有添加到工程设置中 2,重复链接链接错误,一个定义体(实现体)被多个CPPP文件包含,导 ...
- 服务器上安装搭建node环境
一.版本 : node v4.4.3 npm v2.15.1 pm2 v1.1.3 express v4.13.4 二.node安装与环境配置 新建node安装目录,与node项目目录 ...
- Tomcat/weblogic session失效时间的几种设置方法
一.在容器中设置tomcat中配置server.xml中定义context时采用如下定义: <Context path="/livsorder" docBase=" ...
- JDK+Tomcat+Eclipse环境搭建过程记录
这学期选了一门公选课叫网络开发工具与技术,主要学习用JSP语言构建网站.在配置环境的过程中遇到不少的坑,于是记录下来,希望能帮到大家. 系统环境:Win10 JDK版本:8u121, JAVA版本1. ...
- Java提取文本文档中的所有网址(小案例介绍正则基础知识)
正则表达式基础以及Java中使用正则查找 定义: 正则表达式是一些用来匹配和处理文本的字符串 正则的基础(先大致了解下) 1. 正则表达式的作用 查找特定的信息(搜索) 替换一些文本(替换) 2. 正 ...
- python基础-2 编码转换 pycharm 配置 运算符 基本数据类型int str list tupple dict for循环 enumerate序列方法 range和xrange
1.编码转换 unicode 可以编译成 UTF-U GBK 即 #!/usr/bin/env python # -*- coding:utf-8 -*- a = '测试字符' #默认是utf-8 a ...
- linux中cut命令
cut命令 cut常用参数 cut命令用来显示行中的指定部分,删除文件中指定字段. 说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的内容输出到标准输出上 ...