rohan13 commited on
Commit
83aabf7
1 Parent(s): e0faf9b

flask executor

Browse files
Files changed (2) hide show
  1. app.py +9 -5
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,16 +1,20 @@
1
  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 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
- max_workers=4
13
- executor = concurrent.futures.ThreadPoolExecutor(max_workers)
 
 
14
 
15
  @app.route('/')
16
  def index():
@@ -22,7 +26,7 @@ def handle_message(data):
22
  question = data['question']
23
  print("question: " + question)
24
 
25
- if executor._work_queue.qsize() >= max_workers:
26
  emit('response', {'response': 'Server is busy, please try again later'})
27
  return
28
 
@@ -31,7 +35,7 @@ def handle_message(data):
31
  response = future.result()
32
  emit('response', {'response': response})
33
  except Exception as e:
34
- print(f"Error processing request: {str(e)}")
35
  emit('response', {'response': 'Server is busy. Please try again later.'})
36
 
37
  if __name__ == '__main__':
 
1
  from flask import Flask, render_template
2
+ from flask_executor import Executor
3
  from flask_socketio import SocketIO, emit
4
  from flask_cors import cross_origin, CORS
5
  from main import run
6
+ from gevent import monkey
7
 
8
+ monkey.patch_all(ssl=False)
9
  app = Flask(__name__)
10
  app.config['SECRET_KEY'] = 'secret!'
11
  socketio = SocketIO(app, cors_allowed_origins="*")
12
  cors = CORS(app)
13
 
14
+ executor = Executor(app)
15
+
16
+ executor.init_app(app)
17
+ app.config['EXECUTOR_MAX_WORKERS'] = 5
18
 
19
  @app.route('/')
20
  def index():
 
26
  question = data['question']
27
  print("question: " + question)
28
 
29
+ if executor.futures:
30
  emit('response', {'response': 'Server is busy, please try again later'})
31
  return
32
 
 
35
  response = future.result()
36
  emit('response', {'response': response})
37
  except Exception as e:
38
+ traceback.print_exc()
39
  emit('response', {'response': 'Server is busy. Please try again later.'})
40
 
41
  if __name__ == '__main__':
requirements.txt CHANGED
@@ -6,5 +6,6 @@ openai==0.27.4
6
  flask==2.2.3
7
  flask-socketio==5.3.3
8
  flask-cors==3.0.10
 
9
  gevent==22.10.2
10
  gevent-websocket==0.10.1
 
6
  flask==2.2.3
7
  flask-socketio==5.3.3
8
  flask-cors==3.0.10
9
+ flask-executor==1.0.0
10
  gevent==22.10.2
11
  gevent-websocket==0.10.1