BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #15608同步于 2016/8/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

[问题]求问大神:协程和yield from实现异步IO

wfygrs
2016/8/20镜像同步6 回复
在廖雪峰的官网上的一段代码,没有搞懂,可不可以解释一下@asyncio.coroutine和yield from的作用,以及如何实现异步IO import threading import asyncio @asyncio.coroutine def hello(): print('Hello world! (%s)' % threading.currentThread()) yield from asyncio.sleep(1) print('Hello again! (%s)' % threading.currentThread()) loop = asyncio.get_event_loop() tasks = [hello(), hello()] loop.run_until_complete(asyncio.wait(tasks)) loop.close()
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
asif12机器人#1 · 2016/8/20
既然使用了异步为什么还要有thread呢? 修饰符没什么用,调试可以追踪状态;yieldfrom 是交出阻塞函数的控制,可以看看文档。还有aiohttp这个库的使用
nuanyangyang机器人#2 · 2016/8/20
就是的。coroutine和thread没有关系。
wfygrs机器人#3 · 2016/8/21
【 在 asif12 的大作中提到: 】 : 既然使用了异步为什么还要有thread呢? : 修饰符没什么用,调试可以追踪状态;yieldfrom 是交出阻塞函数的控制,可以看看文档。还有aiohttp这个库的使用 文档有什么推荐吗,网上百度的yield from 解释都不好。可不可以解释下异步和thread,有点懵。
nuanyangyang机器人#4 · 2016/8/21
https://docs.python.org/3/reference/expressions.html#yieldexpr
asif12机器人#5 · 2016/8/21
我是看这个入门的: http://aosabook.org/en/500L/a-web-crawler-with-asyncio-coroutines.html 这个应用: http://aiohttp.readthedocs.io/en/stable/
wfygrs机器人#6 · 2016/8/21
非常感谢