Forums

flask web application with time consuming function

Since I dont know how to search I hope no one bothers if I drop the question here. I have a flask web application with a time consuming function which takes around 50 sec to finish. During that time the site ist not responding. How can I deal with this issue? (btw.:I have a paid plan)

regards, Stephan

You could have your web app tell a scheduled task to do the work and then when it's done you could have it tell the web app that it's done. Then you could have some javascript in your page that polls a view to see whether it is done and shows a little "please wait" sign while it's not done.

Hi, Problem is that the function can be triggered by the user. Its solving and displaying the result. BR, Stephan

I'm not sure I understand why you think that's an issue.

Hi Stephan -- just to clarify a bit: with your paid plan, you have two worker processes handling requests to your website.

When a user makes a request, the worker process that handles it is tied up processing, and doesn't handle any other requests until it's done. That means that you still have one process handling requests -- but then, if another user makes a request, then both will be tied up and your website will be unresponsive. (There's also a bit of logic in the way request dispatching works that means that requests from a particular browser tend to be sent to the same worker process -- so you might see situations where it looks like the site is generally unresponsive when only one of the long-running requests is being handled, if you check its responsiveness in a different tab in the same browser as the one where you've triggered the long-running request. You can see something closer to the reality of what's happening if you use two separate browsers.)

Anyway, clearly you could "solve" the problem by adding extra workers -- you can upgrade to an account with more workers from the "Account" page. This is probably the easiest solution, but might be expensive.

The alternative that Glenn is suggesting is that you have a scheduled task that is always running by using this trick. It keeps an eye on a database table, looking for new entries. When your website wants one of these time-consuming things done, you hit a view that writes something to that table, and then display something like "working on it" to the user. Your code on the front-end then keeps hitting a view saying "is it done yet?" repeatedly. That view, when the task is completed, can return the result.

In the background, when the scheduled task sees a new job in the database table, it pulls it off, does the calculations, and then writes the result back to the database for the polling view to pick up.

Does that make more sense?