【发布时间】:2018-12-12 14:02:31
【问题描述】:
我正在处理Python Pyramid rest api,在其中一个请求中我需要处理一个 excel,并且对于它的每一行,我都会获取 GPS 坐标并进行大量验证,这意味着这个唯一的请求可能需要大约 10 分钟来处理,我需要使用 json 向调用者返回响应。我不能通过其他任何东西的 WebSocket 发送它。
我的问题是:如何增加此请求的超时时间?这是我的方法:
@view_config(route_name='client_api_upload',
request_method='POST',
renderer='json',
permission='upload client')
def client_api_upload(request):
client_id = request.POST['client_id']
filename = request.POST['file'].filename
input_file = request.POST['file'].file
extension = os.path.splitext(filename)[1]
new_file_name = str(uuid.uuid4())+extension
file_path = os.path.join(tempfile.gettempdir(), new_file_name)
with open(file_path, 'wb') as output_file:
shutil.copyfileobj(input_file, output_file)
importer = ClientBuildingImporter(client_id=client_id, db_session=request.dbsession)
importer.import_building_from_excel(excel_file_path=file_path)
return importer.import_result
感谢您的帮助
【问题讨论】:
-
HTTP 的设计不是这样工作的,它假定响应总是及时可用的。也许有一个端点用于提交文件,另一个端点用于获取结果?客户端需要轮询结果端点,直到它真正获得有用的结果。也就是说,你怎么能花 10 分钟来解析一个 excel 文件?它们只能有一百万行(或类似的行),因此您最多应该能够在几秒钟内翻阅所有行