Of course the code in the example doesn't make sense and in fact I can't
think of any reason why one would call Task.set_result() or
Task.set_exception(). The question is whether we should disable them
explicitly. I think a single StackOverflow question isn't enough to believe
that this is a common enough misconception to bother. But I'm also not dead
set against such a change -- it does seem pretty absurd. Somebody is
probably subclassing Task and playing games that would break if we changed
this -- but then again, I somehow believe that any subclass of Task that
does something like that is already going beyond the documented contract.


On Wed, Sep 3, 2014 at 5:27 PM, Victor Stinner <[email protected]>
wrote:

> 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
>



-- 
--Guido van Rossum (python.org/~guido)

Reply via email to