Spaces:
Runtime error
Runtime error
thread pool executor
Browse files
app.py
CHANGED
|
@@ -2,15 +2,14 @@ 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 |
-
import
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
app.config['SECRET_KEY'] = 'secret!'
|
| 9 |
-
socketio = SocketIO(app, cors_allowed_origins="*"
|
| 10 |
cors = CORS(app)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
thread_count = 0
|
| 14 |
|
| 15 |
@app.route('/')
|
| 16 |
def index():
|
|
@@ -19,21 +18,11 @@ def index():
|
|
| 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 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
emit('response', {'response': response})
|
| 39 |
|
|
|
|
| 2 |
from flask_socketio import SocketIO, emit
|
| 3 |
from flask_cors import cross_origin, CORS
|
| 4 |
from main import run
|
| 5 |
+
import concurrent.futures
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
app.config['SECRET_KEY'] = 'secret!'
|
| 9 |
+
socketio = SocketIO(app, cors_allowed_origins="*")
|
| 10 |
cors = CORS(app)
|
| 11 |
|
| 12 |
+
executor = concurrent.futures.ThreadPoolExecutor(max_workers=4)
|
|
|
|
| 13 |
|
| 14 |
@app.route('/')
|
| 15 |
def index():
|
|
|
|
| 18 |
|
| 19 |
@socketio.on('message')
|
| 20 |
def handle_message(data):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
question = data['question']
|
| 22 |
print("question: " + question)
|
|
|
|
| 23 |
|
| 24 |
+
future = executor.submit(run, question)
|
| 25 |
+
response = future.result()
|
| 26 |
|
| 27 |
emit('response', {'response': response})
|
| 28 |
|