forked from miguelgrinberg/python-engineio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreading.py
More file actions
48 lines (39 loc) · 1.13 KB
/
Copy paththreading.py
File metadata and controls
48 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from __future__ import absolute_import
import queue
import threading
import time
try:
from simple_websocket import Server, ConnectionClosed
_websocket_available = True
except ImportError: # pragma: no cover
_websocket_available = False
class WebSocketWSGI(object): # pragma: no cover
"""
This wrapper class provides a threading WebSocket interface that is
compatible with eventlet's implementation.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
self.ws = Server(environ)
return self.app(self)
def close(self):
return self.ws.close()
def send(self, message):
try:
return self.ws.send(message)
except ConnectionClosed:
raise IOError()
def wait(self):
try:
return self.ws.receive()
except ConnectionClosed:
return None
_async = {
'thread': threading.Thread,
'queue': queue.Queue,
'queue_empty': queue.Empty,
'event': threading.Event,
'websocket': WebSocketWSGI if _websocket_available else None,
'sleep': time.sleep,
}