Robert Hairgrove Robert Hairgrove - 10 months ago 53
Ajax Question

Aborting a long-running CGI script written in C++ from client with AJAX request

I have a CGI script written in C++ which performs a relatively simple loop in a brute force evaluation of a scheduling-type problem. Parameters are collected from a database, and the CGI script is called from the web browser in JavaScript using an XMLHttpRequest passing the parameters in a POST request. This works fine. But sometimes it takes too long, and I would like to have the user be able to abort the script by clicking on a button in the browser while the script is running.

I have resorted to polling a little file from inside the CGI script. The file can contain either '0' or '1', indicating that the script should abort. This works, too. However, the file I/O itself takes a lot of time relatively speaking, and I was wondering if there is more elegant way of doing this? I can only check it every 4 or 5 million iterations, otherwise I run into problems. I can live with that, but I am wondering if I could do it with an environment variable, for example?

Thanks for any tips!

Answer Source

After some preliminary tests, it seems that the shared memory facilities offered by PHP shmop will be the easiest and safest to use in the current server environment available to me. Many thanks to Adrian Colomitchi who pointed me in the right direction (RAM disk == shared memory)!