Flask异步执行任务

from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(4)

app = Flask(__name__)

@app.route('/jobs')
def run_jobs:
    # 交由线程去执行耗时任务
    executor.submit(long_task, 'hello', 123)
    return 'long task running.'

# 耗时任务
def long_task(arg1, arg2):
    print("args: %s %s!" % (arg1, arg2))
    sleep(5)
    print("Task is done!")

if __name__ == '__main__':
    app.run

发表评论

邮箱地址不会被公开。 必填项已用*标注