Flask接受任务并异步处理同时多线程处理任务

常用flask任务,记录比较,后期方便查看

import time
from flask import Flask
from threading import Thread
from concurrent.futures import ThreadPoolExecutor


app = Flask(__name__)


def get_information_page(auth,count):
    success = 0
    fail = 0
    while 1:
        time.sleep(5)
        print(auth,success,fail)
        if success == count:
            break




def task_main(auth,count,threadCount):
    # 空列表
    t_list = []
    # 创建多个线程并启动线程
    for i in range(threadCount):
        t = Thread(target=get_information_page,args=(auth,count,))
        t_list.append(t)
        t.start()
    # 回收线程
    for i in t_list:
        i.join()



@app.route('/<auth>/<count>/', methods=['GET'])
def index(auth,count):
    thread_master = int(count / 10)
    thread_child = count % 10
    if thread_master:
        ThreadPoolExecutor().submit(task_main,auth, thread_master,10)
    if thread_child:
        ThreadPoolExecutor().submit(task_main, auth, thread_child, 1)
    return 'ok'



if __name__ == '__main__':
    app.run()

猜你喜欢

转载自blog.csdn.net/weixin_38927522/article/details/131793304