首页 > 解决方案 > 异步函数中的变量未在 while-True 循环中重新评估

问题描述

我制作了一个虚拟服务器来测试我的 websockets 应用程序。它侦听subscription消息,然后通过套接字提供有关这些订阅的信息。

类的subscriptions属性在初始化时是空的,应该在listen()函数接收订阅消息时填满。但是,似乎self.subscriptionsintalk()从未附加到,使其陷入无限的 while 循环并且从不传输消息。

await asyncio.sleep(1)通过在 for 循环后添加一行来解决问题。但为什么?self.subscriptions每次启动for循环时不应该重新评估吗?

下面的代码:

class DummyServer:
    def __init__(self):
        self.subscriptions = []

    def start(self):
        return websockets.serve(self.handle, 'localhost', 8765)

    async def handle(self, websocket, path):
        self.ws = websocket
        listen_task = asyncio.ensure_future(self.listen())
        talk_task = asyncio.ensure_future(self.talk())

        done, pending = await asyncio.wait(
            [listen_task, talk_task],
            return_when=asyncio.FIRST_COMPLETED
        )

        for task in pending:
            task.cancel()

    async def listen(self):
        while True:
            try:
                msg = await self.ws.recv()
                msg = json.loads(msg)
                await self.triage(msg)  # handles subscriptions
            except Exception as e:
                await self.close()
                break

    async def talk(self):
        while True:
            for s in self.subscriptions:
                dummy_data = {
                    'product_id': s
                }
                try:
                    await self.send(json.dumps(dummy_data))
                except Exception as e:
                    await self.close()
                    break

            await asyncio.sleep(1)  # without this line, no message is ever sent

标签: pythonwebsocketpython-asyncio

解决方案


在你的函数开始时,subscriptions是空的并且for不评估正文。因此,您的协程实际上与以下内容相同:

async def talk(self):
    while True:
        pass

while循环不包含“上下文切换点”,这意味着asyncio事件循环基本上挂在那里,永远执行阻塞的while循环。

添加await sleep()打破魔法圈;甚至await sleep(0)可以提供帮助。

聪明的代码可能应该asyncio.Condition与 结合使用self.subscriptions,但这超出了您原始问题的范围。


推荐阅读