Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

412
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda