python - 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
解决方案
根据文档,您不能等待任何正常功能:
如果一个对象可以在等待表达式中使用,我们就说它是一个可等待对象。可等待对象主要分为三种类型:协程、任务和期货
您必须使用使用异步功能创建的库,例如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
推荐阅读
- verilog - vvp时verilog中的无限循环
- javascript - 如何在内部将每个数组元素传递给javascript中“map”函数内部使用的回调函数
- digital-ocean - 我正在使用 python-digitalocean 在 DO 上创建液滴。我无法将 ssh 密钥附加到液滴
- elasticsearch - Elasticsearch /all/ 请求
- python - 在可能出现重复值的python中从另一个列表中减去一个列表
- ruby-on-rails - 如何为元编程生成的方法自动生成存根
- python - f-string 中的星号(星号)有什么作用?
- python - 如何在 Sqlite 表中查找子字符串?
- python - 如何传递一个空的熊猫查询
- c++ - 如何判断哪个派生类对象称为基类函数