python – 为什么在使用具有asyncio的协同程序列表解析时得到不同的结果?

栏目: Python · 发布时间: 6年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/29334054/why-am-i-getting-different-results-when-using-a-list-comprehension-with-coroutin

我最初有一些代码将结果汇总到列表中.当我重构这个代码来使用列表构成时,我得到了意想不到的结果:

import asyncio

@asyncio.coroutine
def coro():
    return "foo"


# Writing the code without a list comp works,
# even with an asyncio.sleep(0.1).
@asyncio.coroutine
def good():
    yield from asyncio.sleep(0.1)
    result = []
    for i in range(3):
        current = yield from coro()
        result.append(current)
    return result


# Using a list comp without an async.sleep(0.1)
# works.
@asyncio.coroutine
def still_good():
    return [(yield from coro()) for i in range(3)]


# Using a list comp along with an asyncio.sleep(0.1)
# does _not_ work.
@asyncio.coroutine
def huh():
    yield from asyncio.sleep(0.1)
    return [(yield from coro()) for i in range(3)]


loop = asyncio.get_event_loop()
print(loop.run_until_complete(good()))
print(loop.run_until_complete(still_good()))
print(loop.run_until_complete(huh()))

如果我运行这个代码,我得到这个输出:

$<a href="https://codeday.me/tag/python">Python</a>3.4 /tmp/test.py
['foo', 'foo', 'foo']
['foo', 'foo', 'foo']
<generator object <listcomp> at 0x104eb1360>

为什么我会获得第三个huh()函数的不同结果?

解决问题的方法是将next(…)代替…返回第三个函数,或者更好地为范围(3)中的i返回列表((coro()的yield)()对于这个想法来说,@zch的信用),或者更好的保持第一个功能.

关键是第二个函数不是发生器.它只是一个普通的函数,返回一个理解生成器.

例如,此代码在生成器外部有效:

values = [(yield x) for x in range(3)]

那么你可以这样做:

next(values)
0
next(values)
1
next(values)
2
next(values)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration: [None, None, None]

装饰器@coroutine然后通过迭代结果使第二个函数成为一个生成器,参见 here ,第143行.

相比之下,第一个和第三个函数实际上是生成器,而@coroutine装饰器只返回自己,参见 here ,第136-137行.在第一种情况下,生成器返回列表(实际引发StopIteration([‘foo’,’foo’,’foo’])).在第三种情况下,它返回理解产生器.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/29334054/why-am-i-getting-different-results-when-using-a-list-comprehension-with-coroutin


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python语言程序设计基础(第2版)

Python语言程序设计基础(第2版)

嵩天、礼欣、黄天羽 / 高等教育出版社 / 2017-2 / 39

本书提出了以理解和运用计算生态为目标的Python语言教学思想,不仅系统讲解了Python语言语法,同时介绍了从数据理解到图像处理的14个Python函数库,向初学Python语言的读者展示了全新的编程语言学习路径。 全书一共设计了25个非常具有现代感的实例,从绘制蟒蛇、理解天天向上的力量到机器学习、网络爬虫,从文本进度条、统计名著人物重要性到图像手绘效果、雷达图绘制,绝大多数实例为作者原创......一起来看看 《Python语言程序设计基础(第2版)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码