[Python-ideas] [Python-Dev] PEP 3156 - Asynchronous IO Support Rebooted

Yuriy Taraday yorik.sar at gmail.com
Wed Jan 9 13:51:09 CET 2013


On Wed, Jan 9, 2013 at 4:13 PM, Richard Oudkerk <shibturn at gmail.com> wrote:

> On 09/01/2013 10:54am, Nick Coghlan wrote:
>
>> You need to combine it with the other LBYL checks (f.done() and
>> f.cancelled()) to be sure it won't throw an exception.
>>
>>      if f.done() and not f.cancelled():
>>          # Since we now know neither TimeoutError nor CancelledError can
>> happen,
>>          # we can check for exceptions either by calling f.exception() or
>>          # by calling f.result() inside a try/except block
>>          # The latter will usually be the better option
>>
>> Just calling f.result() is by far the most common, but the other can
>> be convenient in some cases (e.g. if you're writing a scheduler that
>> needs to check if it should be calling send() or throw() on a
>> generator).
>>
>
> Which goes to show that it cannot be used with LBYL.
>
> For exception() to be usable with LBYL one would need to be able to check
> that exception() returns a value without having to catch any exceptions --
> either from exception() or from result().
>
> But you can only check that exception() doesn't raise an error by calling
> result() to ensure that it does raise an error.  But then you might as well
> catch the exception from result().
>
> And the idea of calling exception() first and then result() if it fails is
> just crazy.
>
> As things stand, exception() is pointless.


exception() will raise only TimeoutError or CancelledError, exceptions from
the Future computation are not raised, they are returned.
So to verify that a Future is properly computed, you should write:

    f.done() and not f.cancelled() and f.exception() is None

and you won't have to catch any exceptions.

-- 

Kind regards, Yuriy.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130109/32570e99/attachment.html>


More information about the Python-ideas mailing list