runserver.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import logging
  2. from django.conf import settings
  3. from django.core.management.commands.runserver import Command as RunCommand
  4. from pycs_socketio import sio
  5. def run_eventlet():
  6. import eventlet
  7. import eventlet.wsgi
  8. from pycs_backend.wsgi import application
  9. listener = eventlet.listen(('', settings.SOCKETIO_PORT))
  10. eventlet.wsgi.server(listener, application)
  11. def run_gevent():
  12. # deploy with gevent
  13. from gevent import pywsgi
  14. from pycs_backend.wsgi import application
  15. try:
  16. from geventwebsocket.handler import WebSocketHandler
  17. pywsgi.WSGIServer(
  18. ('', settings.SOCKETIO_PORT), application,
  19. handler_class=WebSocketHandler).serve_forever()
  20. except ImportError:
  21. pywsgi.WSGIServer(('', settings.SOCKETIO_PORT), application).serve_forever()
  22. class Command(RunCommand):
  23. help = 'Run the Socket.IO server'
  24. def handle(self, *args, **options):
  25. if sio.async_mode == 'threading':
  26. super(Command, self).handle(*args, **options)
  27. elif sio.async_mode == 'eventlet':
  28. run_eventlet()
  29. elif sio.async_mode == 'gevent':
  30. run_gevent()
  31. elif sio.async_mode == 'gevent_uwsgi':
  32. logging.warning('Start the application through the uwsgi server. Example:')
  33. logging.warning('uwsgi --http :5000 --gevent 1000 --http-websockets '
  34. '--master --wsgi-file pycs_backend/wsgi.py --callable '
  35. 'application')
  36. else:
  37. logging.error('Unknown async_mode: ' + sio.async_mode)