Spaces:
Runtime error
Runtime error
Commit
·
e2cf778
1
Parent(s):
24f8788
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, g
|
2 |
+
from threading import Thread
|
3 |
+
from queue import Queue
|
4 |
+
import requests
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Configurable parameter for number of workers
|
10 |
+
WORKERS = int(os.getenv('WORKERS', 2))
|
11 |
+
|
12 |
+
API_ENDPOINT = os.getenv('API_ENDPOINT', 'https://actual-api-endpoint')
|
13 |
+
|
14 |
+
class Worker(Thread):
|
15 |
+
def __init__(self, queue):
|
16 |
+
Thread.__init__(self)
|
17 |
+
self.queue = queue
|
18 |
+
|
19 |
+
def run(self):
|
20 |
+
while True:
|
21 |
+
data, callback = self.queue.get()
|
22 |
+
try:
|
23 |
+
response = requests.post(API_ENDPOINT, data=data)
|
24 |
+
callback(response)
|
25 |
+
finally:
|
26 |
+
self.queue.task_done()
|
27 |
+
|
28 |
+
|
29 |
+
@app.before_first_request
|
30 |
+
def initialize_workers():
|
31 |
+
g.queue = Queue()
|
32 |
+
|
33 |
+
for _ in range(WORKERS):
|
34 |
+
worker = Worker(g.queue)
|
35 |
+
worker.daemon = True
|
36 |
+
worker.start()
|
37 |
+
|
38 |
+
|
39 |
+
@app.route('/api', methods=['POST'])
|
40 |
+
def queue_api():
|
41 |
+
data = request.get_json()
|
42 |
+
|
43 |
+
def callback(response):
|
44 |
+
return response.content
|
45 |
+
|
46 |
+
g.queue.put((data, callback))
|
47 |
+
|
48 |
+
g.queue.join()
|
49 |
+
|
50 |
+
return callback
|
51 |
+
|
52 |
+
|
53 |
+
if __name__ == '__main__':
|
54 |
+
app.run()
|