Flask accepts tasks and processes them asynchronously while multithreading them

Commonly used flask tasks, record comparison, easy to view later

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()

Guess you like

Origin blog.csdn.net/weixin_38927522/article/details/131793304