【发布时间】:2019-11-22 14:53:26
【问题描述】:
我有一个网络端点供用户上传文件。 当端点收到请求时,我想运行一个后台作业来处理文件。
由于作业需要时间来完成,我希望将作业 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 方法永远不会被调用。无法理解为什么。
我不确定这是否是正确的方法,如果我遗漏了什么,请帮忙。
【问题讨论】:
标签: python flask python-multithreading aiohttp python-asyncio