Spaces:
Runtime error
Runtime error
added multi-threading
Browse files
app.py
CHANGED
@@ -2,12 +2,16 @@ from flask import Flask, render_template
|
|
2 |
from flask_socketio import SocketIO, emit
|
3 |
from flask_cors import cross_origin, CORS
|
4 |
from main import run
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
app.config['SECRET_KEY'] = 'secret!'
|
8 |
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='gevent')
|
9 |
cors = CORS(app)
|
10 |
|
|
|
|
|
|
|
11 |
@app.route('/')
|
12 |
def index():
|
13 |
return render_template('index.html')
|
@@ -15,9 +19,22 @@ def index():
|
|
15 |
|
16 |
@socketio.on('message')
|
17 |
def handle_message(data):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
question = data['question']
|
19 |
print("question: " + question)
|
20 |
response = run(question)
|
|
|
|
|
|
|
|
|
21 |
emit('response', {'response': response})
|
22 |
|
23 |
|
|
|
2 |
from flask_socketio import SocketIO, emit
|
3 |
from flask_cors import cross_origin, CORS
|
4 |
from main import run
|
5 |
+
import threading
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
app.config['SECRET_KEY'] = 'secret!'
|
9 |
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='gevent')
|
10 |
cors = CORS(app)
|
11 |
|
12 |
+
thread_lock = threading.Lock()
|
13 |
+
thread_count = 0
|
14 |
+
|
15 |
@app.route('/')
|
16 |
def index():
|
17 |
return render_template('index.html')
|
|
|
19 |
|
20 |
@socketio.on('message')
|
21 |
def handle_message(data):
|
22 |
+
global thread_count
|
23 |
+
|
24 |
+
with thread_lock:
|
25 |
+
if thread_count >= 4:
|
26 |
+
emit('response', {'response': 'Server is busy. Please try again later.'})
|
27 |
+
return
|
28 |
+
else:
|
29 |
+
thread_count += 1
|
30 |
+
|
31 |
question = data['question']
|
32 |
print("question: " + question)
|
33 |
response = run(question)
|
34 |
+
|
35 |
+
with thread_lock:
|
36 |
+
thread_count -= 1
|
37 |
+
|
38 |
emit('response', {'response': response})
|
39 |
|
40 |
|