I have some simple code code made with Python 3.4's asyncio using call_later. The code should print, waits 10 seconds, and then print again (but instead raises TypeError when end() should be excecuted, see below):
import asyncio @asyncio.coroutine def begin(): print("Starting to wait.") asyncio.get_event_loop().call_later(10, end()) @asyncio.coroutine def end(): print("completed") if __name__ == "__main__": try: loop = asyncio.get_event_loop() loop.create_task(begin()) loop.run_forever() except KeyboardInterrupt: print("Goodbye!") Gives the error:
Exception in callback <generator object coro at 0x7fc88eeaddc8>() handle: <TimerHandle when=31677.188005054 <generator object coro at 0x7fc88eeaddc8>()> Traceback (most recent call last): File "/usr/lib64/python3.4/asyncio/events.py", line 119, in _run self._callback(*self._args) TypeError: 'generator' object is not callable From what I can tell from the docs (https://docs.python.org/3/library/asyncio-task.html#coroutine), call_later takes a coroutine object, which is obtained by calling a coroutine function. This appears to be what I've done, but asyncio does not call end() properly.
How is this supposed to be done?