python websockets,多个请求,为什么只有请求被接收?



我想测试一下,如果我在同一时刻使用协程发送多个请求,会导致服务器端接收到损坏的数据。

我的测试是基于示例代码:https://websockets.readthedocs.io/en/stable/intro.html

不知何故,对于下面的代码,服务器端只接收一个请求?有人有什么见解吗?thx服务器(这基本上是来自websockets入门网页的相同代码):

#!/usr/bin/env python3                                                                                                                                                                         
  
# WS server example                                                     
  
import asyncio                                                          
import websockets                                                       
  
async def hello(websocket, path):                                       
name = await websocket.recv()                                       
print(f"< {name}")                                                  
  
greeting = f"Hello {name}!"                                         
  
await websocket.send(greeting)                                      
print(f"> {greeting}")                                              
  
start_server = websockets.serve(hello, "localhost", 8765)               
  
asyncio.get_event_loop().run_until_complete(start_server)               
asyncio.get_event_loop().run_forever()      

客户端,我创建了1000个任务,并计划尽快运行它们:

#!/usr/bin/env python3                                                  
  
# WS client example                                                     
  
import asyncio                                                          
import websockets                                                       
  
uri = "ws://localhost:8765"                                             
connection = None                                                       
async def hello():                                                      
global connection                                                   
name = "What's your name? "                                         
  
await connection.send(name)                                         
print(f"> {name}")                                                  
  
async def main():                                                       
global connection                                                   
connection = await websockets.connect(uri)                          
  
#asyncio.run(main())                                                    
if __name__ == "__main__":                                              
loop = asyncio.get_event_loop()                                     
loop.run_until_complete(main())                                     
loop.run_until_complete(asyncio.wait(                               
[hello() for i in range(1000)], return_when=asyncio.ALL_COMPLETED
))                                              

解决方案是使用循环。

我找到了原因:服务器端处理程序应该使用循环,以便协程在收到第一个请求后不会立即完成。

您链接的文档还包括服务器代码下面的这一段:

在服务器端,websockets为每个WebSocket连接执行一次处理程序协程hello。当处理程序协程返回时,它关闭连接。

您链接的客户端代码创建一个连接并在该连接上发送消息。在客户端发送第一条消息后,服务器关闭连接,因此您尝试发送的下999条消息将在已关闭的连接上发送。

如果您更新hello处理程序以包含循环,您将看到所有消息。

import asyncio                                                          
import websockets                                                       
  
async def hello(websocket, path):
while True:
name = await websocket.recv()                                       
print(f"< {name}")                                                  
  
greeting = f"Hello {name}!"                                         
  
await websocket.send(greeting)                                      
print(f"> {greeting}")                                              
  
start_server = websockets.serve(hello, "localhost", 8765)               
  
asyncio.get_event_loop().run_until_complete(start_server)               
asyncio.get_event_loop().run_forever()

最新更新