You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bazarr/libs/engineio/async_drivers/gevent.py

84 lines
2.9 KiB

5 years ago
import gevent
from gevent import queue
from gevent.event import Event
try:
# use gevent-websocket if installed
5 years ago
import geventwebsocket # noqa
SimpleWebSocketWSGI = None
except ImportError: # pragma: no cover
# fallback to simple_websocket when gevent-websocket is not installed
from engineio.async_drivers._websocket_wsgi import SimpleWebSocketWSGI
5 years ago
class Thread(gevent.Greenlet): # pragma: no cover
"""
This wrapper class provides gevent Greenlet interface that is compatible
with the standard library's Thread class.
"""
def __init__(self, target, args=[], kwargs={}):
super().__init__(target, *args, **kwargs)
5 years ago
def _run(self):
return self.run()
if SimpleWebSocketWSGI is not None:
class WebSocketWSGI(SimpleWebSocketWSGI): # pragma: no cover
"""
This wrapper class provides a gevent WebSocket interface that is
compatible with eventlet's implementation, using the simple-websocket
package.
"""
def __init__(self, handler, server):
# to avoid the requirement that the standard library is
# monkey-patched, here we pass the gevent versions of the
# concurrency and networking classes required by simple-websocket
import gevent.event
import gevent.selectors
super().__init__(handler, server,
thread_class=Thread,
event_class=gevent.event.Event,
selector_class=gevent.selectors.DefaultSelector)
else:
class WebSocketWSGI: # pragma: no cover
"""
This wrapper class provides a gevent WebSocket interface that is
compatible with eventlet's implementation, using the gevent-websocket
package.
"""
def __init__(self, handler, server):
self.app = handler
5 years ago
def __call__(self, environ, start_response):
if 'wsgi.websocket' not in environ:
raise RuntimeError('The gevent-websocket server is not '
'configured appropriately. '
'See the Deployment section of the '
'documentation for more information.')
self._sock = environ['wsgi.websocket']
self.environ = environ
self.version = self._sock.version
self.path = self._sock.path
self.origin = self._sock.origin
self.protocol = self._sock.protocol
return self.app(self)
5 years ago
def close(self):
return self._sock.close()
5 years ago
def send(self, message):
return self._sock.send(message)
5 years ago
def wait(self):
return self._sock.receive()
5 years ago
_async = {
'thread': Thread,
'queue': queue.JoinableQueue,
'queue_empty': queue.Empty,
'event': Event,
'websocket': WebSocketWSGI,
5 years ago
'sleep': gevent.sleep,
}