Skip to content

python-socketio not handling events concurrently with gevent_uwsgi #90

@stoarca

Description

@stoarca

Minimal test case:
app.py

import gevent.monkey
gevent.monkey.patch_all()

import logging
import socketio
import time

from flask import Flask

sio = socketio.Server(logger=logging, engineio_logger=logging, async_mode='gevent_uwsgi')

class ChatNamespace(socketio.Namespace):
  def on_delayed_ping(self, sid, data):
    print('got socket ping %s' % data)
    time.sleep(1)
    print('returning socket pong %s' % data)
    self.emit('delayed_pong', data, room=sid)

chatNamespace = ChatNamespace('/chat')
sio.register_namespace(chatNamespace)

app = Flask(__name__)

@app.route('/delayed_ping')
def ping():
  print('got ping')
  time.sleep(1)
  print('returning pong')
  return 'pong'

app = socketio.Middleware(sio, app)

Start the server with uwsgi --http :5000 --gevent 1000 --http-websockets --master --wsgi-file app.py --callable app

Then, run test.js with node test.js

var rp = require('request-promise');
var io = require('socket.io-client');

socket = io.connect('http://localhost:5000/chat', {
  'reconnection delay': 0,
  'reopen delay': 0,
  'force new connection': true,
});

var httpCall = function(i) {
  rp.get('http://localhost:5000/delayed_ping').then(function(data) {
    console.log(i + ' ' + data);
  });
};

var socketCall = function(i) {
  socket.emit('delayed_ping', {i: i});
};
for (var i = 0; i < 100; ++i) {
  httpCall(i);
  socketCall(i);
}

The regular http requests are all run in parallel and take a total of approximately 1 second for all of them. The socket calls, on the other hand, are run one at a time (100 seconds total). It's quite possible I've misconfigured something, but my understanding is that all of the socket calls should run in parallel as well.

Note that if I run node test.js on two separate terminals, the server processes two requests at a time (i.e. it runs in parallel across multiple sockets). So it's just within a single socket that it does not parallelize.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions