I am trying to understand the behavior of the code below. I tried to register a callback function trampoline to be called in an event loop using asyncio module. The trampoline got itself is called multiple times, which I am sure why that happened please. My expectation was the callback trampoline will call itself only once later after 0.5 seconds?
import asyncio import datetime loop = asyncio.get_event_loop() def print_date(): print(datetime.datetime.now()) def trampoline(name: str = "") -> None: print("name:", end=" ") print_date() loop.call_later(0.5, trampoline, name) loop.call_soon(trampoline()) loop.call_later(8, loop.stop) loop.run_forever() Output: I also have an exception, which also I am not sure about:
name: 2022-11-11 16:29:04.335313 Exception in callback None() handle: <Handle> Traceback (most recent call last): File "C:\Anaconda3\lib\asyncio\events.py", line 81, in _run self._context.run(self._callback, *self._args) TypeError: 'NoneType' object is not callable name: 2022-11-11 16:29:04.850481 name: 2022-11-11 16:29:05.363303 name: 2022-11-11 16:29:05.872649 name: 2022-11-11 16:29:06.383004 name: 2022-11-11 16:29:06.898766 name: 2022-11-11 16:29:07.413907 name: 2022-11-11 16:29:07.915795 name: 2022-11-11 16:29:08.428609 name: 2022-11-11 16:29:08.939604 name: 2022-11-11 16:29:09.454111 name: 2022-11-11 16:29:09.966438 name: 2022-11-11 16:29:10.470832 name: 2022-11-11 16:29:10.981675 name: 2022-11-11 16:29:11.496746 name: 2022-11-11 16:29:12.011841 Process finished with exit code 0
trampolinetakes place?loop.call_soon(trampoline)right?