in other words, you want a way to communicate with the process that already exists? aka IPC, there are a lot of ways to do this. the absolute fastest way being shared memory. but using a database, or unix sockets, would be easier to implement. here's an example using SQLite:
whenever convenient to process messages, do this:
while(NULL!==($message=check_for_message())){//handle all messages
echo "got a mesage!:";
var_dump($message);
}
and the "check_for_message" function:
//returns string("message") if there is a message available.
// else returns NULL
//Warning, do not try to optimize this function with prepared statements, unless you know what you're doing, in SQLIte they will lock the database from writing.
function check_for_message(){
static $db=false;
if($db===false){
$db=new PDO('sqlite:ipc.db3','','',array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$db->exec(
'
CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT);
'
);
register_shutdown_function(function()use(&$db){
$db=NULL;
unset($db);
unlink("ipc.db3");
});
}
$message=$db->query("SELECT id,message FROM messages LIMIT 1",PDO::FETCH_ASSOC);
foreach($message as $ret){
$db->query("DELETE FROM messages WHERE id = ".$db->quote($ret['id']));
return $ret['message'];
}
return NULL;
}
and to send a message:
example usage:
foreach($argv as $arg){
sendmessage($arg);
}
function:
function sendmessage(string $message){
$db=new PDO('sqlite:ipc.db3','','',array(
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$db->query('INSERT INTO messages (message) VALUES('.$db->quote($message).');');
}
notes: i think you could optimize that function by using WAL-mode sqlite and prepared statements. i think you could optimize it even more by putting it in shared memory by using PDO::ATTR_PERSISTENT , but afaik, that's hackish using undocumented features and i wouldn't expect it to work for things like HHVM PHP. on unixes (*BSD, Mac OS X, Linux, etc), using a unix socket would be faster anyway. it'd be even faster to use raw shared memory, but implementing message queues in shared memory is somewhat tricky..
you could also look into installing a signal handler, for instance, SIGUSR1 to indicate that there's a message waiting, see http://php.net/manual/en/function.pcntl-signal.php