内容简介:代码日志版权声明:翻译自: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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning XML with DOM and Ajax
Sas Jacobs / Apress / 2006-06-05 / USD 39.99
Don't waste time on 1,000-page tomes full of syntax; this book is all you need to get ahead in XML development. Renowned web developer Sas Jacobs presents an essential guide to XML. Beginning XML with......一起来看看 《Beginning XML with DOM and Ajax》 这本书的介绍吧!