当前位置: 代码迷 >> 综合 >> concurrent.futures多线程使用
  详细解决方案

concurrent.futures多线程使用

热度:100   发布时间:2023-11-08 04:10:58.0

1.ThreadPoolExecutor多线程

from flask import Flask,request,json
app=Flask(__name__)
@app.route('/test',methods=['GET', 'POST'])
def test():a = [1, 2, 3, 4, 5, 6, 7, 8]a= request.json.get('a')executor = Cfutures.ThreadPoolExecutor(max_workers=4) executor.map(reade_value, a)#map中a为listexecutor.submit(reade_value, a)#submit中a为单独参数,如果使用submit添加多个线程时需要for a in list:b={'a':'success'}return json.dumps(b)
def reade_value(a):time.sleep(5)print(a)
if __name__ == '__main__':app.run(host='0.0.0.0', port='5005', debug=True,threaded = True,processes = True)
 

2.ProcessPoolExecutor 多进程

from flask import Flask,request,json
app=Flask(__name__)
@app.route('/test',methods=['GET', 'POST'])
def test():a = [1, 2, 3, 4, 5, 6, 7, 8]a= request.json.get('a')executor = Cfutures.ThreadPoolExecutor(max_workers=4)executor.submit(reade_value, a)#submit中a为单独参数,如果使用submit添加多个进程时需要for a in list:b={'a':'success'}return json.dumps(b)
def reade_value(a):time.sleep(5)print(a)
if __name__ == '__main__':app.run(host='0.0.0.0', port='5005', debug=True,threaded = True,processes = True)