0

I am currently working on an application ROFFLE, I may not be very good in terming correctly, What I am able to do right now?
User goes on a website, he clicks on a button and an ajax request is done to python file (test.py) but when he exits, the request is aborted and the processing done till yet has gone waste

What I want to do?
As user clicks the button, the processing starts. The script should not be killed even if the user leaves the webpage. In simple words, the Javascript part should be limited to trigger/queue the python script to execute (with input provided online) which has to be deployed by a web server that supports it via CGI

How can this be implemented?

Please note:
1. This is a web application and cannot be a software

1 Answers1

0

From the question I read that you have already managed to run a Python script in a web server via CGI and you already know how to do an HTTP (ajax) request from your JavaScript to that web service.

When you now close the page in your browser (or an excavator cuts your line), the backend python script is not terminated. In fact, how should the backend even know that you have closed the page? Your Python script is still running in the backend, but no one will be left to capture the HTTP response of the web server and display it to you.

However, when you want to start some kind of demon, a program that is supposed to run in the backend for a very long time, then your Python script should spin off that task via a Popen in a variant that keeps the child process alive, even when the script has returned it's HTTP response (and possibly even the web server has shut down).

This pattern is sometimes used to remote control little servers that mock IoT devices in test environments. Just start and stop the simulation via some fire-and-forget HTTP requests triggered from a simple interactive web page.

flaschbier
  • 3,910
  • 2
  • 24
  • 36
  • my system is an ML (Machine Learning) base, so what happens is that person sends an AJAX request, the program is triggered, now the person after 15 minutes of triggering (if process isn't completed yet) is given a unique code, now when he comes after an an hour or so, if the process was complete, he would get the results else tell the user to wait for some more time –  Feb 21 '16 at 17:13
  • Cool. Then it might be advisabel that your backend starts an asyncronous process and responds to the user with a message like "Visit us again in ... minutes to see the result." right away. You will be left, however, with the issue to properly re-identify the user when she comes back. – flaschbier Feb 21 '16 at 17:31
  • But how can that be implemented? –  Feb 21 '16 at 18:00
  • With "it", you mean the re-identification, I assume. Well, you can do all from seting a cookie in the user's browser up to a full fledged self-registration user management with password. Or you can return a magic number that the user can enter later ("Visit us again and enter '6F5RHGTK73P' to see the result" – which is cool regarding simplicity as well as security and privacy when you enforce `https` on the site). It depends on the situation, but I would at least consider the latter. – flaschbier Feb 21 '16 at 18:13
  • I am afraid re-identification of user is not much of deal but how would I access the script itself, maybe something like a code snippet or perhaps a google chat/email? –  Feb 21 '16 at 18:18
  • Starting a background script is comparatively simple. See [this answer](http://stackoverflow.com/a/31483173/3991164) on how to do it using the `subprocess` module. Regarding how to embed the "starter" in a webserver, I have also given starting points in the original answer. There will be also issues regarding choosing and accessing the database, but this is stuff for many, many more questions ;) – flaschbier Feb 21 '16 at 18:33