Reputation: 149
I have two PHP files, which are run for a long time (no execution limit). One of the files is used to checking some data on remote server. To make it more effective I run a few instances of file #1. The second file is supposed to perorm action, once a valid response is received by any of the files #1.
The question - how to pass data from one executing file to other? First idea was to save data to file by file #1 and with file #2 check the file modification time regularly. The problem is that it causes much load on the server and if I limit it by using sleep function, the minimum sleep time I can achieve is 1ms (time quantum of Linux system - is there any way to change it)? So - maybe anybody has any ideas how else to pass information from one process to other?
$last = filemtime('action.txt');
while(!$stop) {
if($last != filemtime('action.txt')) { echo microtime(true); $stop = 1; }
clearstatcache('action.txt');
usleep(100);
$i++;
}
Anything below < usleep(1000) works the same - it sleeps 1ms and only 1000 iterations are done per second.
Regards, Jonas
Upvotes: 0
Views: 2033
Reputation: 7935
Lots of feedback.
First am I correct in assuming that you are hosting these files in a web server and invoking them by "opening" them in a web browser. I am asking this because of the "no execution limit" mentioned in the question. you should consider writing and running your PHP scripts on the CLI, unless there is some limitation of your hosting architecture. Ignore this if you're already running them as CLI scripts.
Second, as others are suggesting you should use TCP/IP sockets API available in PHP to connect between the two scripts, with one script being the server and the second being the client. If you are not familiar with this, it may take a while before you are able to do it correctly. so you can ignore this as well for now.
Third, why is it a problem that the minimum duration of sleep is 1ms? it is confusing me because, on the one hand, you are saying that you are using sleep because without it the load is high. On the other hand, you want to sleep for less than 1ms... ideally, you should be sleeping for 5 seconds, 10 seconds or more depending on how often is the file going to be updated, and what kind of time you have to respond to it. if you want near instanteous response, you can try to do what the Linux tail utility does. This is what the pear inotify extension (mentioned by another poster before me) does. If this is a very serious application and you need really fast response time, you should consider using a real-time operating system. an RTOS will give you sleep with less than 1ms.
Fourth, if these two scripts are going to be on the same server (and always on the same server), you can consider other options like shared memory.
Upvotes: 1
Reputation: 798606
Use streams to create or connect to a socket and then select on the socket.
Upvotes: 1
Reputation: 3915
use PHP inotify extensions (http://www.php.net/manual/en/intro.inotify.php, the extension URL is http://pecl.php.net/package/inotify) . It will alert your process when a file is changed. Else you can use a simple socket to raise the second script.
Upvotes: 1