-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
53 lines (38 loc) · 1013 Bytes
/
Copy pathserver.py
File metadata and controls
53 lines (38 loc) · 1013 Bytes
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
49
50
51
52
53
"""
Repeater server
===============
Reads samples from a file and sends them over
a WebSocket
"""
from flask import Flask
from flask_sockets import Sockets
import gevent
import gevent.queue
app = Flask(__name__)
sockets = Sockets(app)
sample_queue = gevent.queue.Queue()
@sockets.route('/echo')
def echo_socket(ws):
while True:
message = ws.receive()
ws.send(message)
@app.route('/')
def main():
return 'Welcome to the sampling server'
@sockets.route('/sampler_input')
def batch_enqueue(ws):
while ws is not None:
gevent.sleep(0.01)
messages = ws.receive()
if messages:
for msg in messages.split():
sample_queue.put(msg)
@sockets.route('/sampler_output')
def batch_dequeue(ws):
while ws is not None:
gevent.sleep(0.01)
if not sample_queue.empty():
sample = sample_queue.get()
while sample[-1] != "}":
sample += sample_queue.get()
ws.send(sample)