I created a hard to track down bug in our code, but do not understand why it occurs. The problem occurs when pushing the same async function multiple times to call soon. It d
In addition to correct explanations by others concerning the error in the lambda
, also note that you don't even need the lambda
. Since do_something
is a coroutine, just calling it will not execute any of its code until the next iteration of the event loop, so you automatically have the effect of a call_soon
. (This is analogous to how calling a generator function doesn't start executing it until you start exhausing the returned iterator.)
In other words, you can replace
self.loop.call_soon(lambda: asyncio.ensure_future(self.do_something(k, v)))
with the simpler
self.loop.create_task(self.do_something(k, v))
create_task
is preferable to ensure_future
when you are dealing with a coroutine.