Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
gevent.py84 linesDownload Raw Back to async_drivers
1import gevent2from gevent import queue3from gevent.event import Event4try:5    # use gevent-websocket if installed6    import geventwebsocket  # noqa7    SimpleWebSocketWSGI = None8except ImportError:  # pragma: no cover9    # fallback to simple_websocket when gevent-websocket is not installed10    from engineio.async_drivers._websocket_wsgi import SimpleWebSocketWSGI11 12 13class Thread(gevent.Greenlet):  # pragma: no cover14    """15    This wrapper class provides gevent Greenlet interface that is compatible16    with the standard library's Thread class.17    """18    def __init__(self, target, args=[], kwargs={}):19        super().__init__(target, *args, **kwargs)20 21    def _run(self):22        return self.run()23 24 25if SimpleWebSocketWSGI is not None:26    class WebSocketWSGI(SimpleWebSocketWSGI):  # pragma: no cover27        """28        This wrapper class provides a gevent WebSocket interface that is29        compatible with eventlet's implementation, using the simple-websocket30        package.31        """32        def __init__(self, handler, server):33            # to avoid the requirement that the standard library is34            # monkey-patched, here we pass the gevent versions of the35            # concurrency and networking classes required by simple-websocket36            import gevent.event37            import gevent.selectors38            super().__init__(handler, server,39                             thread_class=Thread,40                             event_class=gevent.event.Event,41                             selector_class=gevent.selectors.DefaultSelector)42else:43    class WebSocketWSGI:  # pragma: no cover44        """45        This wrapper class provides a gevent WebSocket interface that is46        compatible with eventlet's implementation, using the gevent-websocket47        package.48        """49        def __init__(self, handler, server):50            self.app = handler51 52        def __call__(self, environ, start_response):53            if 'wsgi.websocket' not in environ:54                raise RuntimeError('The gevent-websocket server is not '55                                   'configured appropriately. '56                                   'See the Deployment section of the '57                                   'documentation for more information.')58            self._sock = environ['wsgi.websocket']59            self.environ = environ60            self.version = self._sock.version61            self.path = self._sock.path62            self.origin = self._sock.origin63            self.protocol = self._sock.protocol64            return self.app(self)65 66        def close(self):67            return self._sock.close()68 69        def send(self, message):70            return self._sock.send(message)71 72        def wait(self):73            return self._sock.receive()74 75 76_async = {77    'thread': Thread,78    'queue': queue.JoinableQueue,79    'queue_empty': queue.Empty,80    'event': Event,81    'websocket': WebSocketWSGI,82    'sleep': gevent.sleep,83}84 
codekingpro/portable-devtools · Team Ai