-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_for_api.py
51 lines (41 loc) · 1.09 KB
/
api_for_api.py
1
2
3
4
5
6
7
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
from flask import Flask, jsonify
import multiprocessing as mp
app = Flask(__name__)
@app.route('/api', methods=['GET'])
def get_data():
data = {
'message': 'Hello, world!',
'status': 'success'
}
return jsonify(data)
@app.route('/long_task', methods=['GET'])
def long_task():
data = {
'message': 'Long_task completer!',
'status': 'success',
'sum': slow_calculation()
}
return jsonify(data)
def slow_calculation():
sum = 0
for i in range(200000000):
sum += i
return sum
class long_subprocess(mp.Process):
def __init__(self, queue):
super(long_subprocess, self).__init__()
self.queue = queue
def run(self):
sum = slow_calculation()
self.queue.put(sum)
@app.route('/long_task_mp', methods=['GET'])
def long_task_mp():
q = mp.Queue()
process = long_subprocess(q)
process.start()
process.join()
return {'message': 'Long_task completer!',
'status': 'success',
'sum': q.get()}
if __name__ == '__main__':
app.run(threaded=True, port=8080)