我有一个Web端点供用户上传文件。 端点收到请求后,我想运行一个后台作业来处理文件。
由于作业需要花费一些时间,因此我希望在后台中运行作业时,将job_id返回给用户以跟踪请求的状态。
我想知道在这种情况下asyncio是否会有所帮助。
import asyncio
@asyncio.coroutine
def process_file(job_id,file_obj):
<process the file and dump results in db>
@app.route('/file-upload',methods=['POST'])
def upload_file():
job_id = uuid()
process_file(job_id,requests.files['file']) . # I want this call to be asyc without any await
return jsonify('message' : 'Request received. Track the status using: " + `job_id`)
使用上面的代码,永远不会调用process_file方法。无法理解原因。
我不确定这是否是正确的方法,如果我缺少某些东西,请提供帮助。