Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax call to Python

If I have the following code:

$('#do-updates').click(function() {
    $.post('/do_updates/', 
       function(response){
          location.reload();
    });
});

And the python function it calls is:

def do_update():
    for item in items:
        do something

The python function I call takes about 2m to finish.

If a user does the following:

1 - Click on the #do-updates button
2 - Navigate away from the page after 10s

What will happen? Will the entire do_updates() function run, because it is called asynchronously? Or will that function time out? If it does timeout, what's a better way to do the do_updates() function in the background so it will finish?

like image 345
David542 Avatar asked Mar 02 '26 05:03

David542


1 Answers

If you dont need result of do_something on frontend you can use some of asynchronus task solution. Celery is probably most popular and simple to use with django. It delegates work to dedicated worker which is running as different process through messaging queue(rabbitmq, redis, ..).

like image 115
Karol Sikora Avatar answered Mar 04 '26 18:03

Karol Sikora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!