The Python program I'm writing needs to start a local PHP script outside of Python's process. The program also needs to pass params to the PHP script. So far this seems to start the script:
os.system( path_to_script_here param param )
However, I'm pretty certain that Python remains running until the PHP script is complete.
I've also looked at the various os.spawn methods and I'm not sure which would be appropriate for my case. Any ideas?
Thanks!
See: How to start a background process in Python?
Related
I have written a python which used to take values from command line and process the same. 5 variables are taken using raw_input() and in each case something is returned to screen.
I want this whole interaction to happen via php program which calls my python program. They are supposed to exchange variables more than one time.
I have read the possible solutions like running the python through php via shell and passing arguments. But I am not sure how once I start my python program I can simply keep on sending variables to it so that my python program reaches its logical end by getting variables from php.
you have to use an IPC mechanism like file, pipe, named pipe, shared memory,...
http://en.wikipedia.org/wiki/Inter-process_communication
You can generally communicate between languages by using common
language formats, and using stdin and stdout [pipe] to communicate
the data.
from: http://www.stackoverflow.com/questions/14047979/executing-python-script-in-php-and-exchanging-data-between-the-two
I have a shell script which is running in background and i want to know the shell script execution completion and send it to the PHP which is running in browser currently.PHP and shell script has no link now.How to get the shell script completion and signal it to the PHP
I understand this is more a conceptual question. Two simplest methods that come into mind are:
1) Use a "flag file" that you create from the shell script. When the file exists, it means the script is done. When there's no file, the script still runs. Or other way around. That's common method for shell scripts anyway.
For more background info on how to create this from your shell script, see http://www.cyberciti.biz/tips/shell-scripting-bash-how-to-create-empty-temporary-file-quickly.html
From your PHP code, you test for the (non)existence of that file. PHP has a lot of functions for that, eq. look into file_exists ( string $filename ), see http://php.net/manual/en/function.file-exists.php
2) From the shell script, use a URL parameter when you call the php script and from your PHP code, you read the parameter and work with that. PHP has a lot of functions for that (eg. look into $_SERVER['QUERY_STRING']). Some more on using the $_SERVER vars, see http://php.net/manual/en/reserved.variables.server.php
Hope this helps...
Here you got everything you need, examples, explanations and more.
http://php.net/manual/en/function.shell-exec.php
i want to have an constantly opened program (wrote in c++ prefferably).
when the php script is acessed, it will be acessed with some variables which will be passed to the active program. then the program will make some calculations, and it will pass other variables back to the php script, which will be echoed (or they can be echoed from the program too, if it is possible). after the php script ends, the program must be active!
i know that there is the command exec, and i can run a program with those params(which can be variables), but i don't want that since the program must run even if there are no active php script at that time.
i hope you understood my problem.
You can use shared memory functions.
I would suggest using sockets to communicate between the C++ program and the PHP script. Therefore your C++ program would act as a server and the PHP script would connect to it with the Socket functions and localhost address. Then you could send your data between the two programs and when the PHP script ends, the C++ program would stay alive and wait for the next connection.
is it possible to launch a php script in background on the webserver with js and let it run even if you change page or not visit the site at all and then get the current status if you call the php script in a second moment?
This php script will process data for hours and sleep for X seconds/minutes for each loops. If what I asked before is possible how can I even get "echos" from it if php will only generated an output only when the script ends?
Maybe this is not a job for PHP?
thank you
EDIT: on a windows machine with apache
It certainly is possible - I have several scripts that run 24/7 written in PHP. Check out Creating Daemons in PHP. It has good info on how to 'daemonize' a php script so that it will run like a service, and it also covers signal handling.
To get debugging output you would redirect to a log file. Do a search on "unix redirect output" as there is a lot of info available.
In Windows it's not much different from UNIX.
First of all, you need to create a PHP script with a run loop. For example, take a look at this: http://code.google.com/p/php-apns/ . This is a PHP "daemon": the main script, PushMonitor.php, runs forever, because it has an infinite loop. It polls a queue at regular intervals, then execute the actions and then wait. Really simple, actually!
The problem, in your case, is that you want to launch the "daemon" from a PHP script.
You may want to look at this: http://robert.accettura.com/blog/2006/09/14/asynchronous-processing-with-php/ (first example code) . You will execute something like launchBackgroundProcess('php myscript.php') .
Note that on the code there's the "start /b" command (and the "&" at the end of the command for UNIX). That is important, because otherwise your process would be killed when the PHP script of the web page is terminated (children process die after parent dies!).
Also, remember that the "php" executable (cli) must be in your path (so you can execute "php" from the command line).
Since the PHP script of the page launching the background process is going to terminate, you can't directly catch the "echoes" in a simple way. My suggestion is to write all output to a file (or a database etc), and then read the contents from that source when necessary.
So, instead of "echo", you will use file_put_contents() etc.
How do I make python (local) run php script on a remote server?
I don't want to process its output with python script or anything, just execute it and meanwhile quit python (while php script will be already working and doing its job).
edit:
What I'm trying to achieve:
python script connects to ftp server and uploads php script (I already have this part of code)
it runs php script (that's part of code i'm asking about)
python script continues to do something else
python script quits (but probably php script still didn't finished its work so i don't want it to end when it'll exit python)
python script quit, php script still continues its task
(I don't plan to do anything with php output in python - python just has to upload php script and make it start working)
Hope I'm more clear now. Sorry if my question wasn't specific enough.
another edit:
Also please note that I don't have shell access on remote server. I have only ftp and control panel (cpanel); trying to use ftp for it.
os.system("php yourscript.php")
Another alternative would be:
# will return new process' id
os.spawnl(os.P_NOWAIT, "php yourscript.php")
You can check all os module documentation here.
If python is on a different physical machine than the PHP script, I'd make sure the PHP script is web-accessible and use urllib2 to call to that url
import urllib2
urllib2.urlopen("http://remotehost.com/myscript.php")
I'll paraphrase the answer to How do I include a PHP script in Python?.
import subprocess
def php(script_path):
p = subprocess.Popen(['php', script_path] )