Hello Spork! (aka “Forking PHP…”)
A few months ago I was tasked with speeding up the upload of assets to the OpenSky CDN, which was taking a few minutes each deploy. I ended up dividing the upload into multiple processes using pcntl_fork() and bringing the total time of the upload down to a matter of seconds.
Since then I’ve been working on wrapping some of the complexities of working with a parent and child processes into an OO library and am happy to announce this experimental library: Spork.
Its usage is pretty straight forward.
<?php
use Spork\ProcessManager;
use Spork\Deferred\DeferredFactory;
$pm = new ProcessManager(new DeferredFactory());
$pm->fork(function() {
// do something in a child process...
echo posix_getpid();
})->then(function($output) {
// do something in the parent process...
printf('Parent %d forked child %d!', posix_getpid(), $output);
});
You can pass any callable into the process manager’s fork() method. In return you get a nice little deferred object which you can use to queue more callables to run after the child process exits. Just like the jQuery object I ♥ so much, there are three basic methods: always(), done(), and fail(), each of which accept a callable as an argument. There is also a then() method, which is a convenience method for adding a done and a fail callback in one method call.
Case Studies
If you end up using Spork I would like to hear about it. Please post your own blog and I’ll link to it, or send me a little description of what you’re doing and I’ll post it here.