首页 > 解决方案 > Python Websockets - 课堂上的等待问题

问题描述

嗨,我正在编写一个简单的视频聊天应用程序服务器,但遇到了无法等待send_to_all()Room 类中的函数的问题。我是 asyncio 的新手,我不知道该怎么做。

我尝试过使用线程并使用我在网上找到的某种 asyncio 东西,所有这些都不起作用

class Room:
     def __init__(self,_id,owner):
          self.owner = owner
          self.id = _id
          self.participants = []
          
          print("[DEBUG] New room has been created")
          
     async def send_msg_to_all(self, message):
          print("[DEBUG] Sending message")
          for participant in self.participants:
               await participant.conn.send(message)

运行上面的代码后,我得到 RuntimeError

RuntimeWarning: coroutine 'Room.send_msg_to_all' was never awaited
  self.send_msg_to_all(str(serialized_partipants_info))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

标签: pythonwebsocketpython-asyncio

解决方案


根据文档,您不能等待任何正常功能:

如果一个对象可以在等待表达式中使用,我们就说它是一个可等待对象。可等待对象主要分为三种类型:协程、任务和期货

您必须使用使用异步功能创建的库,例如asyncore。请记住,您不能等待已经等待的异步调用。我自己没有使用 asyncore 包,所以你必须阅读它的用法。但在一般情况下,您必须定义单独的异步函数,然后像这样收集它们:

async def async_method1(*args):
  #do your thing

async def async_method1(*args):
  #do your thing

async def async_method1(*args):
  #do your thing

async def all_my_calls(*args):
  reslist = await asyncio.gather(
        async_method1(*args),
        async_method2(*args),
        async_method3(*args))
  # do stuff with your reslist


asyncio.run(all_my_calls(*args))

请记住,根据您的逻辑,您可能不需要任何输入*args或输出reslist。我只是将它们包含在我的代码中,因此您将知道如何传递参数并在需要时处理结果。

编辑

正如 Tomek Pulkiewicz 在评论中已经提到的那样,异步仅用于普通套接字。如果你想使用异步网络套接字,请使用asyncore-wsgi


推荐阅读