Surya Kasturi
Surya Kasturi

Reputation: 4753

How to invoke a python script after successfully running a Django view

Lets say I have a view page(request) which loads page.html.

Now after successfully loading page.html, I want to automatically run a python script behind the scene 10 - 15 sec after the page.html loaded. How it is possible?

Also, is it possible to show the status of the script dynamically (running/ stopped/ Syntax Error..etc)

Upvotes: 0

Views: 437

Answers (3)

Jonas Geiregat
Jonas Geiregat

Reputation: 5442

Celery might come in handy for such use cases. You can start a task (or script as you call them) from a view (even with a delay, as you want). Sending status reports back to the browser will be harder unless you opt for something like WebSockets but that's highly experimental right now.

Upvotes: 0

surfeurX
surfeurX

Reputation: 1252

Runing a script from the javascript is not a clean way to do it, because the user can close the browser, disable js ... etc. instead you can use django-celery, it let you run backgroud scripts and you can check to status of the script dynamically from a middleware. Good luck

Upvotes: 1

Scotty
Scotty

Reputation: 2510

You could add a client-side timeout to AJAX back to the server 10-15 sec later. Point it to a different view and execute your script within that view. For example:

function runServerScript() {
    $.get("/yourviewurlhere", function(data) {
        // Do something with the return data
    });
}
setTimeout("runServerScript()", 10000);

If you want status to be displayed, the client would have to make multiple requests back to the server.

Upvotes: 0

Related Questions