from flask import Flask
from time import sleep
from concurrent.futures import ThreadPoolExecutor
# DOCS https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
# 创建线程池执行器
executor = ThreadPoolExecutor(2)

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))
 for i in range(10):
     sleep(1)
     print("Task is done!")


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

  

多思考也是一种努力,做出正确的分析和选择,因为我们的时间和精力都有限,所以把时间花在更有价值的地方。