Call python script from PHP - parallel requests problem - php

I call Python script via exec blocking function inside php file which is served by Nginx. It works as expected. Python script executes Selenium using webdriver Chrome and returns xml output.
The problem is when I create more requests (2 and more) in the same time. First request is processed (nginx, php, python) and the second waits for finish of the first. They are not parallel executed.
Python script executed directly from shell can be parallel executed without problems.

You have two option.
Multi-Threading
PHP does not support threads natively, but there is this solution. It is still experimental and it requires PHP 7.2+, but if you are already using PHP 7.2+ it will work like a charm.
Multi-Processing
You can make new processes by forking. You can read more about forking in PHP in this answer and more about it as a concept here.
Or you can also do it in the current way with exec(), but you have to put & at the end of the command, so the PHP might pass it to the console and it can run in the background and run the 2nd one.
There is a third way using MQ Engines, but I cannot help with that, but you can read about it too if you are interested. Hope something of these helps.

PHP isn't directly asyncronious, use the pcntl extencion and look at this

Related

Calling C program with PHP

I’ve written a small database engine in C that works by reading commands you input in the console and it outputs the result.
Is there any way of me writting some PHP code that could send arguments to the console and recieve the output back to PHP without restarting the compiled program.
Is there a better way of doing this?
You say you want the PHP to send and receive messages to your program without restarting the compiled program.
So I don't think using shell_exec or proc_open will work how you want, since these commands both load a fresh instance of the compiled program.
Instead, I suggest you look into sockets, and how you would rewrite your database engine to use those instead of STDIN/STDOUT. Then you can use PHP's socket functions to communicate between your applications. And you'll have just one instance of your compiled program running in the background, even with multiple hits to your PHP script.

Executing Python scripts in PHP: Different methods of execution?

I want to execute a Python script from PHP and apparently there are two completely different methods to do it.
The first method is to install Python on Wampserver (which I am using). The method described here involves extra files (mod_wsgi) and some modification (httpd.conf). I tried this method and Wampserver started failing (localhost not available) which was resolved by removing mod_wsgi.
The second method is by simply using the shell_exec command (example) which executes the Python script without any problems. I was a little surprised at how easy it was to do it and I have no idea why it works. I assume that the shell used is that of the current server's. Does this mean that I can also use installed external libraries in that script (e.g. OpenCV / cv2)?
What are the differences between the two methods and why is method 1 so complicated?

Calling Python script in PHP post PHP 5.4

Basically I want to call a simple python script from PHP. I have a PHP page that is adding requests from an html page to a Mysql table, and I'd like this PHP page to call a python script which email administrators to my site once the PHP has finished. I'm currently trying to use the command exec('sudo python /var/www/alertAdmins.py'). However, after reading the PHP documentation it seems that exec requires safe mode, which has been removed as of PHP 5.4 to my understanding. Is there an alternative function I can use? Any workarounds?
No, exec() does not require Safe Mode.
It used to have additional restrictions when Safe Mode was on, though.

Execute another PHP file without waiting for it to finish its execution

I have StartServer.php file that basically starts a server if it is not already started. Everything works perfect except that the StartServer.php will hang forever waiting for the shell_exec()'d file Server.php to finish its execution that it never does.
Is there a way to execute a PHP file and just forget about it -- and not wait for its execution to finish?
Edit: It has to work on Windows and Linux.
This should help you - Asynchronous shell exec in PHP
Basically, you shell_exec("php Server.php > /dev/null 2>/dev/null &")
You'll need something like the pcntl functions. Only problem is that this is a non-windows extension, and not suitable to run inside a web server. The only other possibility I can think of is to use the exec function and fork the current process manually (as suggested in dekomotes's answer), doing OS detection to figure out the command needed. Note that this approach isn't much different to using the pcntl functions (in Linux, at least) - the ampersand character causes the second script to be run inside different process. Multi-threaded / multi-process programming isn't well supported in PHP, particularly when running inside a web server.
I think it's traditional to let the server detach itself form the parent process, ie to "daemonize" itself, rather than having the script starting the server detach itself. Check the server you're starting to see if it has a daemon-option.
If you've written the server yourself, in PHP, you need to detach it. It looks somehting like this:
posix_setsid(); //Start a new session
if(pcntl_fork()) {exit();} //Fork process and kill the old one
I think this works on Windows too. (Not tested.)

Calling fcsh from PHP script

My question is whether or not Flex's fcsh can be called from within a PHP script. Here is the background:
I have been created a simple process that creates a simple quiz/tutorial by converting a text file into a .mxml file and compiling to a .swf file using the mxmlc compiler. This works well from the command line, but I wanted to make the process easier by creating a web-interface to do this. My initial attempts with PHP's exec() function have not worked. The Python scripts I use to create the .mxml file work fine (using exec()), but I have not been able to get the mxmlc compiler to work.
After searching on the Web and on this site, I believe that using fcsh (instead of mxmlc) may be the way to go. Using fcsh would certainly compile the .mxml file faster (after the first run), and I think that fcsh can be launched as a service that might be able to be called from PHP.
On the other hand, maybe I am approaching this the wrong way. Would it be better to write a Flex application that calls fcsh and avoid using PHP?
Edit: Using fcshctl as hasseg suggested in his answer below worked very well. Thanks Ali.
The problem with calling fcsh from within scripts is that it works as an interactive shell instead of taking command-line arguments, compiling, and returning an exit status. There are different ways to get around this, which I've listed in this blog post of mine, where I mainly talk about fcshctl (which is my own solution for this,) but at the bottom of the post I've also listed other similar solutions to get fcsh integrated into nonstandard build workflows.
There are a few other ways in php to execute an external script. They are exec(), passthru(), system(), and backticks i.e. the key to the left of the 1 key. Each one has a different purpose and return mechanism.
You may have to put the command that executes your executable into a script and call that script via one of these functions.
Is there a particular reason why you can't use mxmlc directly? It seems like it would be easier to call than fcsh. Just specify all your compiler options in a XML file run it like mxmlc -load-config path/to/config.xml. You can find an example of the XML configuration format in FLEX_HOME/frameworks/flex-config.xml.

Categories