Why does python asyncio loop.call_soon overwrite data?

后端 未结 3 1543
暗喜
暗喜 2021-01-27 11:51

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

3条回答
  •  醉话见心
    2021-01-27 12:40

    This has nothing to do with the async code, but with the lambda you're creating in your loop. When you write lambda: asyncio.ensure_future(self.do_something(k, v)), you're creating a closure that accesses the variables k and v from the enclosing namespace (and self too, but that's not a problem). When the lambda function is called, it will use the values bound by those names in the outer scope at that time of the call, not the values they had when the lambda was defined. Since k and v change value on each iteration of the loop, that's causing all the lambda functions to see the same values (the last ones).

    A common way to avoid this issue is to make the current values of the variables default values for arguments to the lambda function:

    self.loop.call_soon(lambda k=k, v=v: asyncio.ensure_future(self.do_something(k, v)))
    

提交回复
热议问题