Hi Alexander,

I don't believe there's any way which which you can check the result
of a callback from the Handle. You would have to use something like a
Future passed into your callback.
You can also use this future to capture any potential exceptions
thrown in the callback.

    def callback(arg, future):
        try:
            result = int(arg)
            future.set_result(result)
        except Exception as e:
            future.set_exception(e)


    if __name__ == "__main__":
        loop = asyncio.get_event_loop()

        print("Schedule callback")
        future = asyncio.Future()  # Future to capture result
        loop.call_later(5, callback, "42", future)

        print("Wait for result")
        print("Result: %d" % loop.run_until_complete(future))


One I've seen to delay scheduling of a coroutine or Future is to just
use `call_later`:

    @asyncio.coroutine
    def test_coro(delay, loop):
        loop.call_later(delay, asyncio.async, mycoro())


The downside to this is you can't capture any result or exception from
`mycoro` since this discards the Task returned by `asyncio.async`.
If you wanted the result of mycoro, then your approach of scheduling a
coroutine which delays the target coroutine seems pretty concise. Just
remember to `yield from` the Task returned from `asyncio.async` when
you need the result.

Happy coding.
David

On Thu, May 7, 2015 at 5:13 PM, Alexander Shorin <[email protected]> wrote:
> Hi,
>
> Excuse me if such trivial questions were already answered, but I failed to
> find any traces of them.
>
> Assume I want to make a delayed call:
>
>     handler = loop.call_later(delay, callback)
>
> How can I check if callback was called for _now_?
> Handler seems only provides a way to cancel a call, not to check it status:
> is called, failed with exception etc.
>
> If the callback failed, how can I get an exception of it?
> loop.set_exception_handler seems is designed to catch everything, but I need
> to catch exact that callback failure.
>
> Bonus: what's the best way to delay call (delay yield from) coroutines? I
> guess is something like that:
>
>     @asyncio.coroutine
>     def yield_later(delay, coro):
>         yield from asyncio.sleep(delay)
>         yield from coro
>
>     asyncio.async(yield_later(delay, mycoro(foo, bar=baz)))
>
> but just want to make sure.
>
> For now, the only solution I see is about wrap callback with additional
> functions in order to catch and process raised exceptions, set an Event in
> order to notify callee about callback call and so on, but doing all this I
> have a strange feel that I miss something oblivious. So do I?
>
> --
> ,,,^..^,,,
>
>

Reply via email to