Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

411
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!