python - 如何使用 asyncio.Protocol 处理断开/重新连接?
问题描述
我正在尝试设置一个将 2 个连接“粘合”在一起的应用程序。我正在尝试正确使用异步的东西。
到目前为止,我是这样做的:
import asyncio
class Protocol1(asyncio.Protocol):
def connection_lost(self, exc):
# should I do something here?
# some stuff
class Protocol2(asyncio.Protocol):
# some other stuff
async def main():
loop = asyncio.get_running_loop()
transport1, protocol1 = await loop.create_unix_connection(lambda: Protocol1(), path=SOCKET_PATH)
on_con_lost = loop.create_future() # is this sound?
transport2, protocol2 = await loop.create_connection(lambda: Protocol2(), host=HOST, port=PORT)
protocol2.procotol1 = protocol1 # maybe this is ugly too?
await on_con_lost
print("Both connection lost?")
asyncio.run(main())
我想了解如何处理重新连接,以防两个连接之一出现故障。你能指出我正确的方向吗?
解决方案
推荐阅读
- html - Angular HTML元素对齐问题
- php - 注意:数组到字符串转换 PHP 错误,SESSION ARRAYS?
- python - 使用 Python 中的有序键列表遍历带有列表的嵌套字典
- python - Scrapy将html元素保存到html文件
- android - 使用 google place api 在 android 中自定义搜索
- git - How to manage absolute file path in visual studio project in source control?
- c# - 使用枚举显示数据时出错
- visual-studio-2015 - GIT 命令致命错误无法访问“https://mygiturl/”:请求的 URL 返回错误:400
- python - 如何使用 find_packages() 打包子目录中的所有文件
- python - 处理来自 python 列表框的错误