I need 14 variables globally whose value can be manipulated by both php and python program.
The python program just wants to read the value of the variable. The PHP program should write the value of the variable depending upon the button pressed in the web server page.
How do i create that variable and use it in raspberry pi with Raspbian OS?
3 Answers 3
I like the Redis solution by @tobyd for some situations. The other option, which is not specific in anyway to Python, would be to structure your script (Python in your case - could be bash, Perl, Ruby) so that it communicates with PHP via STDIN and STDOUT. You can call your script from PHP using exec()
and store the results in a PHP variable:
$python_return = exec("script.py {$arg1} {$arg2}");
In the example $arg1
and arg2
are variables in PHP that you want to go into your Python, and $python_return
stores what you get back. The return will be a string, so you may or may not need to do some processing on it to convert type on the PHP side. The Python, in this case, has to be set-up to take command line arguments via STDIN and to write the result that you want back to its STDOUT.
Perhaps use environment variables via setenv and getenv. Both Python and PHP support environment variables.
Python has support within the os module. os.getenv() and os.putenv(). See the on-line documentation for details.
Alternatively create your own system by reading/writing values in an agreed format to a local file.
-
1This won't work because environment variables are copy inherited at creation. E.g., I create one,
export foobar=42
. I start process A and process B. Both of them can readfoobar
and get 42. Then, process A setsfoobar
to 99. Process B checks it and guess what? It is still 42, because process B's environment is not the same as process A's, although they may both have the same origin.goldilocks– goldilocks2017年11月22日 17:07:05 +00:00Commented Nov 22, 2017 at 17:07 -
In my case, i need the data to only by read by the python program. I need PHP to write the data in it and Python to read that data. Is it possible ?Shyam– Shyam2017年11月22日 17:38:48 +00:00Commented Nov 22, 2017 at 17:38
-
Not if they are both persistent processes, or one is not a child of the other. I.e., it will work if the PHP program changes the value and then starts an instance of the python program -- but in that case you might as well just provide them as arguments to the python program.goldilocks– goldilocks2017年11月23日 17:15:11 +00:00Commented Nov 23, 2017 at 17:15
Communication between two running processes is called inter-process communication (IPC) and as per the linked wikipedia article there are many approaches to it.
Note that if you can have the PHP process start the python script each time this value is needed, then you can pass it in as an argument, and you do not need IPC. However, if they are both running independently, then you do.
Probably the simplest approach that will work in this context is writing the information to a file. It does have a few drawbacks, but they may not matter:
Privacy: Unless you create a user or group that only those two processes will use, the information in the file can potentially be read (and altered...) by other processes.
"Race conditions", such that the data read may be garbled if the reading processes reads at the same time as the writing process writes. This can be solved via signals, although that is not ideal if there is no need for synchronisation (meaning, it is a good solution if you want the reader to read right away when the writer writes, but less good if you just want the reader to read whenever it need to use the information). A more flexible method is via file locks, which is a voluntary system on linux (meaning it is only effective if the reader cooperates by checking for a lock; you can't lock a file such that it can't be accessed, although you could do something of the sort by exploiting permissions). A quick search seems to indicate that both PHP and python have implementations for this.
Two things to consider if you do this:
Using a tmpfs backed location. These exist only in RAM, so there is no overhead for accessing the SD card. I don't have a Raspbian system at hand so I am not sure which directories are mounted that way (the storage is created at boot, and will be lost when the system shuts down), but
mount | grep tmpfs
should tell you. The/tmp
directory is ideal since it is world writable. It and/or/run
(or/run
subdirectories) should betmpfs
. Make sure you look into the permissions. You can also have such directories created at boot bysystemd
via a service file, although the easiest way would be via/etc/fstab
.If the 14 variables is just a bunch of integers, it is pretty simple to read them in. If it is more complicated than that, you may want to look into using a portable format such as JSON; I am sure python and PHP both have modules or built-in commands for handling this.