Imran Teli
Imran Teli

Reputation: 53

Flask app takes long time to respond by that time browser connection gets reset

I am new to web development and I have started learning with Flask.

I have written a small app which takes form input from user and saves it in background text file, everything goes well till here.

After this I redirect user to another link, after redirection the app runs a system command which takes almost 15-20 seconds to respond. By the time the app responds I get a connection reset on my browser. Although the background tasks completes after that.

The issue happens when the user gets redirected to below code(url). Connection gets reset before it reaches the return statement. Please let me know as how should I hold the session between browser and app until the app responds to the browser.

@app.route('/running', methods=['GET'])
def running():
    os.system('echo $(date +[%Y/%m/%d_%H:%M:%S])  >> /root/Tas/logs/SN_up_$(date +"%Y-%m-%d").log && /root/Tas/scripts/SN_upgrade.py &>> /root/Tas/logs/SN_up_$(date +"%Y-%m-%d").log')
    return 'Data processed'

Upvotes: 2

Views: 2008

Answers (1)

Midnighter
Midnighter

Reputation: 3881

If you are not actually interested in returning the result of your system command, then I would suggest off-loading that task via a queue, for example, using Flask-Celery. You can then return immediately after submitting the task to the queue. If you want, you can send the user a unique URL where they can check the result later or inform them via e-mail.

The other option is to try and speed up your command since 20 seconds is way beyond any time users are willing to wait. You also have no influence on the user's timeout.

Upvotes: 2

Related Questions