Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

414
Vistas
Understanding how Python websocket keepalive mechanism works

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.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda