Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

64 строки
1.8KB

  1. import importlib
  2. import sys
  3. import gevent
  4. try:
  5. import geventwebsocket # noqa
  6. _websocket_available = True
  7. except ImportError:
  8. _websocket_available = False
  9. class Thread(gevent.Greenlet): # pragma: no cover
  10. """
  11. This wrapper class provides gevent Greenlet interface that is compatible
  12. with the standard library's Thread class.
  13. """
  14. def __init__(self, target, args=[], kwargs={}):
  15. super(Thread, self).__init__(target, *args, **kwargs)
  16. def _run(self):
  17. return self.run()
  18. class WebSocketWSGI(object): # pragma: no cover
  19. """
  20. This wrapper class provides a gevent WebSocket interface that is
  21. compatible with eventlet's implementation.
  22. """
  23. def __init__(self, app):
  24. self.app = app
  25. def __call__(self, environ, start_response):
  26. if 'wsgi.websocket' not in environ:
  27. raise RuntimeError('You need to use the gevent-websocket server. '
  28. 'See the Deployment section of the '
  29. 'documentation for more information.')
  30. self._sock = environ['wsgi.websocket']
  31. self.environ = environ
  32. self.version = self._sock.version
  33. self.path = self._sock.path
  34. self.origin = self._sock.origin
  35. self.protocol = self._sock.protocol
  36. return self.app(self)
  37. def close(self):
  38. return self._sock.close()
  39. def send(self, message):
  40. return self._sock.send(message)
  41. def wait(self):
  42. return self._sock.receive()
  43. _async = {
  44. 'threading': sys.modules[__name__],
  45. 'thread_class': 'Thread',
  46. 'queue': importlib.import_module('gevent.queue'),
  47. 'queue_class': 'JoinableQueue',
  48. 'websocket': sys.modules[__name__] if _websocket_available else None,
  49. 'websocket_class': 'WebSocketWSGI' if _websocket_available else None,
  50. 'sleep': gevent.sleep
  51. }