Not able to understand how the ping pong is handled for python websockets package. The following is a simple fastapi websocket server and a python client. I expect keepalive timeout error but that does not happen.
FastAPI server:
from fastapi import FastAPI, WebSocket
import asyncio
app = FastAPI()
@app.get("/")
async def get():
return "Hello"
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
data = await websocket.receive_text()
await websocket.send_text('test')
await asyncio.sleep(45)
await websocket.send_text(f"Stop")
Python Client
import websockets
import asyncio
async def communicate():
async with websockets.connect("ws://localhost:8000/ws",
ping_timeout=2,
) as websocket:
await websocket.send("Tom")
while True:
response = await websocket.recv()
print(response)
if response == "Stop":
break
if __name__ =="__main__":
asyncio.get_event_loop().run_until_complete(communicate())
Expected Outcome
test -- < Keepalive timeout error >
Actual Outcome:
test
Stop
--> Could someone explain how the ping-pong mechanism works and does the number of CPU cores or available uvicorn workers affect ping-pong mechanism.
Thank you in advance.