Reputation: 181
What I want to do is the following: make a server carry a program which analyses a picture (which is sent to the web server) and returns a result, the program already exists and is written in C# and C++. For interest, this program analyzes a pattern/grid to determine how much pressure is exerted on a metallic object, and takes no more than one second to do its analysis.
I would like to know:
Upvotes: 1
Views: 115
Reputation: 20487
The simple solution to this problem is the run your program synchronously inside your web request (this means that your web page has to wait for the program to deliver its results). This means that, in your PHP page (or a controller, if you are using an MVC-like architecture) you use system()
or similar to run your program on the server. This will probably output results to standard output, or maybe to a file or a database. If your program takes a second to run, this is probably an acceptable delay, as long as you have a low number of users.
To do this:
stdout
or elsewhere, depending on how your console program returns its analysisThat said, a better way to run this it is to run a job server, so that your program is run asynchronously (this means that your job server accepts a request to run the program, and it will be run when a slot becomes free). This will mean your web page will run faster, and your server won't get overloaded with too many people running your program at the same time. The downside is that the architecture of your system is more complicated, and you'll have to change your UI to cope with unprocessed jobs.
If you are interested in setting up a job server, consider something like Gearman. You'll have to poll the server to see if a job has been processed, or have your job process write a completion flag to your database, which can be checked trivially by your web script.
Upvotes: 0
Reputation: 23787
I'd really do this with PHP instead at webserver level.
PHP has a function named shell_exec
. This command can invoke a program.
$result = shell_exec("./your_program ".escapeshellargs($image_path));
Under the condition that the webserver has the rights to execute the program (should be chmod mode 755).
(P.s. This should work on *nix, for Windows I am unsure)
(If you aren't able to compile your program on the server, cross-compile it and then upload it)
Upvotes: 1