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?


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.

| improve this answer | |

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.

PHP getenv and putenv.

Alternatively create your own system by reading/writing values in an agreed format to a local file.

| improve this answer | |
  • 1
    This 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 read foobar and get 42. Then, process A sets foobar 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 Nov 22 '17 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 Nov 22 '17 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 Nov 23 '17 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 be tmpfs. Make sure you look into the permissions. You can also have such directories created at boot by systemd 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.