from concurrent.futures import ThreadPoolExecutor
from flask import Flask, request
import json
from time import sleep
executor = ThreadPoolExecutor(max_workers=4)
app = Flask(__name__)
def task(name):
print(f"Hello {name}")
@app.route("/", methods=["POST"])
def main():
request_dict = json.loads(request.data)
p1 = request_dict["p1"]
p2 = request_dict["p2"]
executor.submit(task, p1, p2) #ubmit(fn, *args, **kwargs)
sleep(3)
return "Get your POST!!!"
if __name__ == '__main__':
app.run()
python多进程
python复制代码
from multiprocessing import Process
def infer(i, filelist):
print(i, filelist)
if __name__ == '__main__':
img_list = []
num_process = 5
num = int(len(img_list) / num_process)
process_list = []
for i in range(num_process):
filelist = img_list[i * num:(i + 1) * num]
if i == num_process - 1:
filelist = img_list[i * num:]
process_list.append(Process(target=infer, args=(i, filelist)))
[p.start() for p in process_list]
[p.join() for p in process_list]