-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (40 loc) · 1.36 KB
/
app.py
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
54
55
56
from queue import Queue
from threading import Event, Thread
from flask import Flask, render_template
from flask_socketio import SocketIO
from streaming_pipeline.runtime import run
from streaming_pipeline.utils import handle_timeout
app = Flask(__name__)
app.config["DEBUG"] = True
socket_io = SocketIO(app, async_mode=None, logger=True, engineio_logger=True)
shared_event = Event()
shared_queue = Queue()
production_queue = Queue()
thread = Thread()
emission_thread = Thread()
@app.route("/")
def index():
return render_template("index.html")
@app.route("/kill")
def kill_stream():
shared_event.set()
return "Set the event to kill stream."
def produce_speed():
while not shared_event.isSet():
global production_queue
pkt = handle_timeout(production_queue, shared_event)
if pkt:
socket_io.emit("f1", {"data": pkt}, namespace="/test")
@socket_io.on("connect", namespace="/test")
def test_connect():
# need visibility of the global thread object
global thread, emission_thread
print("Client connected")
if not thread.isAlive():
print("Starting Thread")
thread = socket_io.start_background_task(
run, shared_queue, production_queue, shared_event
)
emission_thread = socket_io.start_background_task(produce_speed)
if __name__ == "__main__":
socket_io.run(app=app)