0

How do I server my Django application using gevent-socketio's SocketIOServer though Apache via uWSGI?

I have the following uWSGI .ini file:

[uwsgi]
socket = 127.0.0.1:3031
master = true
processes = 2
env = DJANGO_SETTINGS_MODULE=demo.settings
module = app:serve

then I have the following app.py:

from gevent import monkey
from socketio.server import SocketIOServer
import django.core.handlers.wsgi
import os
import sys

monkey.patch_all()

PORT = 3031

os.environ['DJANGO_SETTINGS_MODULE'] = 'demo.settings'

def serve():
    application = django.core.handlers.wsgi.WSGIHandler()
    SocketIOServer(('', PORT), application, namespace="socket.io").serve_forever()

But it just keeps on loading, basically my problem is how do I tell uWSGI to use SocketIOServer when serving?

Marconi
  • 3,601
  • 4
  • 46
  • 72

1 Answers1

2

It is not clear if you want uWSGI to serve both or you want an additional process with the socketio server.

generally you cannot mix blocking apps (like django) with non-blocking (like gevent-based) in the same process and even if you are using monkey patching your database adapter will not be monkeypatched (unless you are using a native python-adapter, and this is uncommon in django).

So i suppose you want to spawn the SocketIOServer as a different process. Just move the last 2 lines out of serve() so the uwsgi importer will parse/run both

roberto
  • 12,723
  • 44
  • 30
  • ah, will try that. I don't care if SocketIOServer is in a separate process as long as it can handle websocket requests from client. Will get back to you, thanks! – Marconi Aug 14 '12 at 07:23
  • Nothing happens, just keeps on loading... :/ – Marconi Aug 14 '12 at 07:38
  • Obviously you cannot bind it to the same uWSGI port, and you have to ensure it gives control back to uWSGI after spawn. Are you sure it is not better to spawn it externally from your main wsgi file ? you can use the attach-daemon option of uWSGI to spawn external processes – roberto Aug 14 '12 at 07:51
  • thanks, but our sysad finally agreed to use nginx + gunicorn. :) – Marconi Aug 18 '12 at 06:39