简体   繁体   English

Python Flask Heroku-图像上传字段(WTForms)使APP超时(时间过长)

[英]Python flask heroku - Image Upload Field (WTForms) makes APP timeout (takes too long)

I have currently an app deployed on heroku. 我目前在heroku上部署了一个应用程序。

It is live since 1 day and I noticed in the logs that users get often an H13 or H28 error on a certain site. 它自1天以来一直处于运行状态,我在日志中注意到,用户经常在某个站点上遇到H13或H28错误。

I already found the problem. 我已经发现问题了。 On this site users are able to upload images (max. 10). 在此站点上,用户可以上传图像(最多10张)。 I use there 1 Input Field for images where I allow multiselect , so the only way to upload multiple images is doing it at once. 我在其中使用允许多选的图像的1输入字段 ,因此上传多张图像的唯一方法是一次完成。

WTForms: WTForms:

bild = FileField("Weitere Bilder hochladen", validators=[FileAllowed(['jpg', 'png', 'jpeg'], 'Nur Bilder im Formal JPG oder PNG')])

Heroku has a default timeout of 30 seconds if the request is not processed. 如果未处理请求,Heroku的默认超时为30秒。 But the process to upload multiple images is too long and therefore heroku interrupts the process and shows the custom error page. 但是上传多张图片的过程太长,因此heroku中断了该过程并显示了自定义错误页面。

My Idea: 我的点子:

To solve this I would disable the multiselect and add 9 additonal FileFields in WTForms, so the user has to upload the images one by one, is this the way to go? 为了解决这个问题,我将禁用多选,并在WTForms中添加9个附加FileField,因此用户必须一个一个地上传图像,这是要走的路吗?

Or shall I add a progress bar and increase the timeout, what is the best practise here? 还是应该添加进度条并增加超时,这里的最佳实践是什么?

EDIT 编辑

Okay I found some solutions, but I would like to use the progress bar and increase the request timeout from heroku. 好的,我找到了一些解决方案,但是我想使用进度条并增加来自heroku的请求超时。

I will try to upload a new procfile: 我将尝试上传新的procfile:

web: gunicorn main:app --timeout 120

Okay I managed to solve this issue, but I made it fast and not very efficient. 好的,我设法解决了这个问题,但是我做的很快,效率不是很高。

The best solution seems to be here: Heroku`s solution 最好的解决方案似乎在这里: Heroku的解决方案

I solved it with an increased timeout for my app. 我为我的应用程序增加了超时来解决它。 Therefore I uploaded the new procfile to my heroku app: 因此,我将新的procfile上传到了我的heroku应用程序中:

web: gunicorn main:app --timeout 120

After that I implement a loading animation to my submit button of the form which makes the long request. 之后,我将加载动画实现到表单的“提交”按钮上,从而产生了较长的请求。 The part I added is in span tag : 我添加的部分在span tag

<button type="submit" class="btn add-btn"> <span class="ani-holder"><span class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></span></span> Zimmer einstellen </button>

Initialy I make the animation to be display: none; 最初,我将要display: none;的动画为display: none; . If the button is clicked, the form will submit. 如果单击该按钮,则将提交表单。 Here I use jQuery to change the display of the loading animation to display: inherit; 在这里,我使用jQuery将加载动画的显示方式更改为display: inherit; and to make it look better I also change the background color of the button: 为了使它看起来更好,我还更改了按钮的背景色:

$('#regi').submit(function(){
    $('.ani-holder').css("display", "inherit");
    $('.add-room-container .add-btn').css("background-color", "#afddfe");           
});

The function is executed while the form submits the request. 该函数在表单提交请求时执行。 After that I redirect url and reload the page, so thats it. 之后,我redirect url并重新加载页面,就是这样。 If the request is fast, then you wont see anything. 如果请求很快,那么您将看不到任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM