Skip to content
This repository has been archived by the owner on Aug 20, 2019. It is now read-only.

Debugging WebSocket connection closures with Bocadillo and Uvicorn

Notifications You must be signed in to change notification settings

florimondmanca/bocadillo-ws-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ws-test

Example application to debug unexpected connection closures when running the code from the Bocadillo tutorial.

Usage

Start the server:

uvicorn app:app --log-level=debug
# OR, if you have Heroku CLI:
heroku local

the following clients are available:

  • asyncio + websockets:

    • Start the client:
    python -m clients.asyncio ws://localhost:8000/echo
    
    • To send messages, type them in the REPL and hit Enter.
  • asyncio + websockets + running input() in the threadpool:

    • Start the client:
    python -m clients.asyncio_threadpool ws://localhost:8000/echo
    
    • To send messages, type them in the REPL and hit Enter.
  • trio + trio-websocket:

    • Create a messages file: touch messages.
    • Start the client:
    python -m clients.trio ws://localhost:8000/echo
    
    • To send messages:
    echo "Hello" >> messages
    • Messages are read asynchronously from the file, so the WebSocket client can send and receive pings and pongs as designed, and the connection stays open.
  • A browser-based JavaScript client: open your browser at http://localhost:8000.

Rationale

The client code in the asyncio + websockets client is using input() to get user input. This is a blocking call, which results in other tasks running on the event loop from running. In particular, this prevents the client from exchanging ping and pong messages with the server. After a while, either the server or the client time out waiting for a pong, and the connection is shut down.

Key learning point: don't block the event loop. Make sure all I/O is asynchronous. This isn't always easy, especially when reading from files or stdin. Alternatives to asyncio such as trio have more tools built-in to help you achieve full-async I/O handling.

About

Debugging WebSocket connection closures with Bocadillo and Uvicorn

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published