There are many excellent reasons why the mechanism I've described won't
change.

I guess you could do this:

try:
    raise RuntimeError
finally:
    <cleanup>

But personally I would just write

<cleanup>
raise RuntimeError

If <cleanup> is a lot of code put it in a helper function.


On Wed, Sep 30, 2015 at 8:29 PM, Oleg K <windspi...@gmail.com> wrote:

> Thank You for explanation,
>
> currently i am rising that exception as is.
>
> but the whole point of calling set_exception() was to mark that task have
> failed for sure,
> and no future code should change that outcome for that task.
>
> but the same time i don't want to exit it immediately, because i need to
> log it somewhere and also
> do a cleanup, and do clean shutdown of other facilities that task involved
> with.
>
> when i raise it - it just exits at once, making cleanup harder to
> implement.
>
> Anyway
> Thanks!
>
>
> On Wednesday, September 30, 2015 at 11:05:14 PM UTC-4, Guido van Rossum
> wrote:
>>
>> You shouldn't do that. What's happening is that when task() returns the
>> Task class wrapping it tries to set the Task object's result, but since
>> you've already set a result, the second set_result() raises
>> InvalidStateError. If you want to raise RuntimeError, just write `raise
>> RuntimeError("msg")` in your function.
>>
>> Note that you could get the same error by using
>> `myself.set_result(None)`. It's not specific to setting an exception, the
>> problem is that the Task instance can't have its result or exeption set
>> multiple times.
>>
>> On Wed, Sep 30, 2015 at 7:13 PM, Oleg K <winds...@gmail.com> wrote:
>>
>>> Hello everyone,
>>>
>>> here is super simple example of one case i ran into:
>>>
>>> import asyncio
>>>
>>> loop = asyncio.get_event_loop()
>>> async def task():
>>>     print('i am task..')
>>>     myself.set_exception(RuntimeError('something bad'))
>>>     print('i keep working')
>>>
>>> myself = loop.create_task(task())
>>> loop.run_forever()
>>>
>>>
>>>
>>> which will produce something strange,
>>> i expected to see just a RuntimeError propagated.
>>> but instead, here is what is happening:
>>>
>>> i am task..
>>>> i keep working
>>>> Exception in callback Task._step()
>>>> handle: <Handle Task._step()>
>>>> Traceback (most recent call last):
>>>>   File "/home/http/Python-3.5.0/lib/python3.5/asyncio/tasks.py", line
>>>> 239, in _step
>>>>     result = coro.send(value)
>>>> StopIteration
>>>>
>>>> During handling of the above exception, another exception occurred:
>>>>
>>>> Traceback (most recent call last):
>>>>   File "/home/http/Python-3.5.0/lib/python3.5/asyncio/events.py", line
>>>> 125, in _run
>>>>     self._callback(*self._args)
>>>>   File "/home/http/Python-3.5.0/lib/python3.5/asyncio/tasks.py", line
>>>> 241, in _step
>>>>     self.set_result(exc.value)
>>>>   File "/home/http/Python-3.5.0/lib/python3.5/asyncio/futures.py", line
>>>> 335, in set_result
>>>>     raise InvalidStateError('{}: {!r}'.format(self._state, self))
>>>> asyncio.futures.InvalidStateError: FINISHED: <Task finished
>>>> coro=<task() done, defined at test_aio_exception.py:4>
>>>> exception=RuntimeError('something bad',)>
>>>>
>>>
>>>
>>> i am sorry in advance if it is just something i don't understand at very
>>> basic level about asyncio.
>>>
>>>
>>> Regards,
>>>
>>> Oleg.
>>>
>>>
>>
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>>
>


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

Reply via email to