lifo / php-ipc
Simple PHP Inter Process Communication (IPC) library
Installs: 4 115
Dependents: 0
Suggesters: 0
Security: 0
Stars: 28
Watchers: 4
Forks: 5
Open Issues: 0
Requires
- php: >=5.3.0
- ext-pcntl: *
This package is auto-updated.
Last update: 2025-03-14 03:44:01 UTC
README
Inter Process Communication
This library is in its infancy. I am adding features to it as I require them in my other projects.
Examples
ProcessPool
The ProcessPool class provides a very simple interface to manage a list of children that return 1 or more results and exit. Each child can return any value that can be serialized.
<?php use Lifo\IPC\ProcessPool; // create a pool with a maximum of 16 workers at a time $pool = new ProcessPool(16); // apply 100 processes to the pool. for ($i=0; $i<100; $i++) { $pool->apply(function($parent) use ($i) { // Each child can write to the $parent socket to send more than a single // result. Make sure you use ProcessPool::socket_send() ProcessPool::socket_send($parent, "$i says the time is " . time()); // Results can be any serializable value ProcessPool::socket_send($parent, array('date' => date('Y-m-d'), 'time' => time())); // Each child can optionally return a result or just use socket_send // as shown above. mt_srand(); // must re-seed for each child $rand = mt_rand(1000000, 2000000); usleep($rand); return $i . ' : slept for ' . ($rand / 1000000) . ' seconds'; }); } // wait for all results to be finished ... while ($pool->getPending()) { try { $result = $pool->get(1); // timeout in 1 second echo "GOT: ", $result, "\n"; } catch (\Exception $e) { // timeout } } // easy shortcut to get all results at once. $results = $pool->getAll();
Advanced ProcessPool Example
<?php use Lifo\IPC\ProcessPool; // Pretend we have open resources like a DB connection... $db = mysqli_connect(...); // create a pool $pool = new ProcessPool(); // set a callback that is called everytime a child is forked $pool->setOnCreate(function() use (&$db) { // reconnect to the DB $db = mysqli_connect(...); }); // create a child $pool->apply(function($parent) use (&$db) { // do stuff in the child; When we exit the $db handle will // close but won't affect the parent since it reconnected // after we were created (our $db handle is not equal to the parent $db) // ... }); $results = $pool->getAll();