yield from
&;await
之间的差异有关async_generator
,我不清楚它与acoroutine
之间差异的后果。(我不知道除了我在标题里写的那个问题之外,我不知道还能怎么问这个问题……)
import asyncio
async def async_generator_spits_out_letters():
yield 'a'
yield 'b'
yield 'c'
yield 'd'
await asyncio.sleep(0)
async def coroutine_prints_messages():
while True:
print('hi')
await asyncio.sleep(2)
def test_it():
print(type(async_generator_spits_out_letters))
print(type(coroutine_prints_messages))
# This is how I choose to do newlines....it's easier for me to read. :[
print(); print()
print(type(async_generator_spits_out_letters()))
print(type(coroutine_prints_messages()))
这样可以得到:
^{pr2}$我搞不清这件事的来龙去脉。。。在
为了让一个
这是为了防止async_generator
生成函数在eventloop中运行,它的输出必须包装在coroutine.
中async_generator
直接将值生成到事件循环中。循环只与任务和未来类似
如果循环从某个任务接收到一个整数或字符串或..任何不是从
future
派生的内容,它将中断。在因此
coroutines
必须:future
s(或{这里是main():
^{pr2}$输出如下:
相关问题 更多 >
编程相关推荐