Skip to content

Commit

Permalink
perf: 使用全局超时
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnserf-Seed committed Oct 26, 2024
1 parent 5180f9c commit b43b003
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions f2/apps/douyin/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ async def fetch_live_danmaku(self, params: LiveWebcast):
)
await self.connect_websocket(endpoint)

server_task = asyncio.create_task(
self.start_server()
) # 在后台启动 WebSocket 服务器
server_task = asyncio.create_task(self.start_server())
try:
return await self.receive_messages()
finally:
Expand Down Expand Up @@ -487,9 +485,15 @@ async def start_server(self):
logger.info(_("[StartServer] [🔒 本地 WebSocket 服务器已关闭]"))

async def _timeout_check(self, server):
timeout = 10 # 设置超时时间,单位为秒
"""
检查本地服务器是否超时无连接
Args:
server: WebSocketServer 对象
"""
while True:
await asyncio.sleep(timeout)
await asyncio.sleep(self.timeout)
if not self.connected_clients:
logger.info(
_(
Expand All @@ -498,8 +502,7 @@ async def _timeout_check(self, server):
)
break
server.close()
await server.wait_closed()
logger.info(_("本地服务器由于超时无连接而关闭"))
# await server.wait_closed()
await self.close_websocket()

async def register_client(self, websocket: WebSocketServerProtocol):
Expand Down

0 comments on commit b43b003

Please sign in to comment.