简体   繁体   中英

javascript 'setTimeout' function stops running after 6 minutes on Flask web server

*edit: I found a bug in Celery task which leads to this problem. So it's unrelated to that piece of JavaScript code blow. Suggest to close this question.

I have this Javascript function running on Flask web server, which checks status of a Celery task. If the task finished, console.log its result, otherwise recheck after 2 seconds.

It runs well if the Celery task finished in less than 6 minutes. However, when it hits the 6 minute mark, this function just stops running. The status of Celery task never updated again, even though I am sure that task is still running on the background, I don't receive any result from Javascript.

Is it a timeout setting of Flask? Any insights will be appreciated.

function update_progress(status_url) {
    // send GET request to status URL
    $.getJSON(status_url, function(data) {
        if (data['state'] != 'PROGRESS') {
            if ('result' in data) {
                console.log(data['result']);
            }
            else {
                // something unexpected happened
                console.log(data['state']);
            }
        }
        else {
            // rerun in 2 seconds
            setTimeout(function() {
                update_progress(status_url);
            }, 2000);
        }
    });
}

you could try use setInterval instead, registering it to a variable.

const updateInterval = setInterval(function(){
 update_progress(status_url);
},2000);

And inside update_progress function, remove the else part and add clearInterval(updateInterval) :

function update_progress(status_url) {
// send GET request to status URL
$.getJSON(status_url, function(data) {
    if (data['state'] == 'PROGRESS') {
        return;
    }

    clearInterval(updateInterval);

    if ('result' in data) {
        console.log(data['result']);
    }
    else {
        // something unexpected happened
        console.log(data['state']);
    }
});

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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