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