Skip to content

Events not handled concurrently #1084

@Adam-D-Lewis

Description

@Adam-D-Lewis

My issue is very similar to this old issue #90, but I'm using uvicorn/FastAPI instead. I expect the events to be handled concurrently, but they seem to be handled synchronously.

To Reproduce

# scratch_server.py
import time
from fastapi import FastAPI
import socketio

app = FastAPI()
app.sio = socketio.AsyncServer(
	async_mode='asgi', 
	cors_allowed_origins='*'
)

app.mount(
	path='/ws', 
	app=socketio.ASGIApp(
		socketio_server=app.sio,
		socketio_path="socket.io",
))

@app.sio.on("ping")
def handle_ping(sid, *args, **kwargs):
    print('got ping')
    time.sleep(1)
    print('returning pong')
    return "pong"
# scratch_client.py
import socketio

sio = socketio.Client()
sio.connect(
	"ws://localhost:8000", socketio_path="/ws/socket.io", transports=["websocket"]
)

for i in range(10):
	sio.emit('ping')

I launch the server with uvicorn scratch_server:app --workers 10. scratch_client.py takes about 10s to run and the output is:

got ping
returning pong
got ping
returning pong
got ping
returning pong
got ping
returning pong
got ping
returning pong
got ping
returning pong
got ping
returning pong
got ping
returning pong
got ping
returning pong
got ping
returning pong

I expect the events to be handled concurrently and so I expect scratch_client.py to run in about 1s and that I'd see 10 "got ping" logs followed by 10 "returning pong" logs. Apologies if this is user error, as I am new to this, but any guidance is greatly appreciated 🙏 .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions