I've been mucking around with asyncio recently, and while I'm beginning to get an intuition for how it works, there's something that I've not been able to do. I'm not sure if it's because I've got the construction wrong, or if there's a reason why what I'm trying to do doesn't make sense.
In short, I want to be able to iterate over a yielding asyncio.coroutine. For example, I'd like to be able to do something like:
@asyncio.coroutine
def countdown(n):
while n > 0:
yield from asyncio.sleep(1)
n = n - 1
yield n
@asyncio.coroutine
def do_work():
for n in countdown(5):
print(n)
loop.run_until_complete(do_work())
However, this throws an exception from the bowels of asyncio. I've tried other things, like for n in (yield from countdown(5)): ... but that also gives a similarly opaque runtime exception.
I can't immediately see why you shouldn't be do something like this, but I'm getting to the limits of my ability to understand what's going on.
So:
- if it is possible to do this, how can I do it?
- if it is not possible, why not?
Let me know if this question's not clear!