Spaces:
Runtime error
Runtime error
Commit
·
879fdec
1
Parent(s):
d87c92c
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,57 @@
|
|
| 1 |
-
from flask import Flask, request,
|
| 2 |
-
from threading import Thread
|
| 3 |
from queue import Queue
|
| 4 |
-
import
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
|
|
|
| 2 |
from queue import Queue
|
| 3 |
+
from threading import Thread
|
| 4 |
import os
|
| 5 |
+
import time
|
| 6 |
+
import requests
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
+
queue = Queue()
|
| 11 |
+
API_ENDPOINT = os.environ.get("API_ENDPOINT", "")
|
| 12 |
+
WORKERS = int(os.environ.get("WORKERS", "1"))
|
| 13 |
+
|
| 14 |
+
def worker():
|
| 15 |
+
while True:
|
| 16 |
+
req = queue.get()
|
| 17 |
+
if req is None:
|
| 18 |
+
break
|
| 19 |
+
|
| 20 |
+
# Forward the request to the API endpoint
|
| 21 |
+
response = requests.request(req['method'], API_ENDPOINT, headers=req['headers'], data=req['data'])
|
| 22 |
+
req['result'] = response.json()
|
| 23 |
+
queue.task_done()
|
| 24 |
+
|
| 25 |
+
@app.route('/api', methods=['GET', 'POST'])
|
| 26 |
+
def handle_request():
|
| 27 |
+
headers = {key: value for key, value in request.headers}
|
| 28 |
+
data = request.get_data()
|
| 29 |
+
method = request.method
|
| 30 |
+
|
| 31 |
+
# Add the request to the queue
|
| 32 |
+
req = {'headers': headers, 'data': data, 'method': method, 'result': None}
|
| 33 |
+
queue.put(req)
|
| 34 |
+
|
| 35 |
+
# Wait until the request has been processed
|
| 36 |
+
while req['result'] is None:
|
| 37 |
+
time.sleep(0.1)
|
| 38 |
+
|
| 39 |
+
return jsonify(req['result']), 200
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
# Start the worker threads
|
| 44 |
+
threads = []
|
| 45 |
+
for i in range(WORKERS):
|
| 46 |
+
t = Thread(target=worker)
|
| 47 |
+
t.start()
|
| 48 |
+
threads.append(t)
|
| 49 |
+
|
| 50 |
+
# Start the Flask app
|
| 51 |
+
app.run(host='0.0.0.0', port=5000)
|
| 52 |
+
|
| 53 |
+
# Stop the worker threads
|
| 54 |
+
for i in range(WORKERS):
|
| 55 |
+
queue.put(None)
|
| 56 |
+
for t in threads:
|
| 57 |
+
t.join()
|