1

I want to know if my code in Flask can stop if it exceeds certain time.

Right now I'm devoloping a code for scraping and sending requests to many APIs and web pages. The runtime can be pretty high if I'm updating many items in it. The issue is that the Flask app is like an internal webpage and I just need to execute the code in it completly.

I really need a big runtime because part of our design in the program is to run as many items as possible.

In the plan of replit there is the option of always on, but is it always on for huge times? If it's not, is there an extra plan to pay or for extending the runtime?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    The issue, of course, is that the user is waiting for your request to complete. He's going to see a spinning wheel until your request completes. If the user's output doesn't depend on the results, then you can spin it out to a thread or process. If the user's output depends on the results, then there are few choices. You can spin off a long-running process and have the web page "refresh" until it completes. – Tim Roberts Apr 10 '23 at 18:47
  • thanks alot. No, the final user doesn't depend on the response. Because I'm the final user and I can see the results on the logs, just for monitoring. – Jose Choriego Apr 10 '23 at 20:36

1 Answers1

0

Yes, you can set a maximum execution time for your code in Flask by using a Python library called "timeout-decorator". This library allows you to set a maximum time limit for the execution of any function or piece of code.

To use this library in your Flask application, you can install it using pip:

pip install timeout-decorator

Then, you can use it to decorate your view function in Flask like this:

 from timeout_decorator import timeout, TimeoutError
@app.route('/long_running_task')
@timeout(60) # set maximum execution time to 60 seconds
def long_running_task():
    # your long running code here
    return 'Task completed successfully!'
Richard
  • 46
  • 4