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

Nick Coghlan ncoghlan at gmail.com
Wed Jan 9 11:54:42 CET 2013


On Wed, Jan 9, 2013 at 8:28 PM, Richard Oudkerk <shibturn at gmail.com> wrote:
> On 09/01/2013 2:31am, Nick Coghlan wrote:
>>
>> The exception() method exists for the same reason that we support both
>> "key in mapping" and raising KeyError from "mapping[key]": sometimes
>> you want "Look Before You Leap", other times you want to let the
>> exception fly. If you want the latter, just call .result() directly,
>> if you want the former, check .exception() first.
>
>
> But how can you do LBYL.  I can't see a way to check that an exception has
> occurred seeing whether result() raises an error: done() tells you that the
> operation is finished, but not whether it succeeded.

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

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list