Hi,

I saw a strange code in a StackOverflow question:
http://stackoverflow.com/questions/23840886/strange-assertionerror-in-asyncio

The code wraps a coroutine into a task and later set manually the
result of the task. Another coroutine waits on this task.

At the exit, the coroutine is not done (it's an unlimited loop) but
the code exits. I tested. With the latest development, no "pending
task destroyed" warning is emited in debug mode.

Does it really make sense to call explicitly Task.set_result() or
Task.set_exception()? Should we still execute the task, or should it
stop immediatly the execution of the task?

Modified example to get the assertion error:
---
import asyncio

@asyncio.coroutine
def greet():
    while True:
        print('Hello World')
        yield from asyncio.sleep(.1)

@asyncio.coroutine
def main():
    future = asyncio.async(greet())
    loop.call_later(.5, lambda: future.set_result(True))
    yield from future
    print('Ready')
    print("exit", future)

loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()
loop.close()
---

=> AssertionError: _step(): already done: <Task finished result=True
coro=<greet() running at x.py:10> wait_for=<Future finished
result=None created at
/home/haypo/prog/python/default/Lib/asyncio/tasks.py:488> created at
x.py:14>, None, None

Victor

Reply via email to