yield from语句
来源:互联网 发布:中国对美国出口数据 编辑:程序博客网 时间:2024/05/04 15:41
result = yield from B()# B是一个coroutine
yield from将调用一个子协程(也就是在一个协程中调用另一个协程)并直接获得子协程的返回结果。
当正在被主线程执行的的coroutine遇到 yield from语句时,当前coroutine会被挂起,yield from 后面的coroutine 会被主线程执行。
例子
import asyncio@asyncio.coroutinedef print_sum(x, y): result = yield from compute(x, y) print("%s + %s = %s" % (x, y, result))@asyncio.coroutinedef compute(x, y): print("Compute %s + %s ..." % (x, y)) yield from asyncio.sleep(1.0) return x + y
在这个例子,当print_sum遇到yield from被挂起时,yield from后面的compute就会被主线程执行
yield from asyncio.sleep(1) #这里CPU并没有休眠,而只是设置了一个定时器,然后就去执行别的代码,等时间到,则再回到这里继续执行。
0 0
- yield from语句
- python3中的yield from语句
- python3.4中yield from语句详解
- yield from
- yield from
- yield语句
- python yield 与 yield from
- 浅谈yield from表达式
- python3 yield from用法
- python yield 和 yield from用法总结
- 迭代器块中yield 语句
- python yield语句
- C# yield语句
- C# yield语句
- Python中的yield from语法
- python yield from的使用
- 异步IO、协程、yield from
- from语句
- 关于wifi连接与断开的监测
- 100. Same Tree
- 第4周项目4—程序分析(4)
- CodeForces-632B-Alice, Bob, Two Teams
- 第九章 多语言环境的支持和多屏幕的适配(2)
- yield from语句
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建(转载)
- J2EE插件开发框架之简述
- 如何在Ubuntu手机中利用百度地图显示一个坐标位置
- maven私服在开发中的具体应用篇
- spring学习笔记(14)引介增强详解:定时器实例:无侵入式动态增强类功能
- shiro配置filterchaindefinitions实现多角色判定方法的重写
- Leet Code OJ 2. Add Two Numbers [Difficulty: Medium]
- 第九章 多语言环境的支持和多屏幕的适配(3)