"Thoughts are things, so why not choose great ones?" - Napoleon Hill
Here’s another quick release. I remember working on this a few years ago. The idea is instead of forking processes, or calling blocking processes (with system, exec, passthru), we spawn children processes. Originally I would communicate with the actual process object, but I found a service with a process pool to be more effective (this way it can handle creation, handling, re-use, destruction, etc) – but a wrapper class could possibly be implemented. Disclaimer: I’m releasing it somewhat unfinished since I’m mostly using Python for my services these days. Feel free to fork it.
Some examples would be handling DNS (since it’s blocking) or encoding video/audio (which can take minutes) or some other command that isn’t non-blocking.
Here’s a quick example that “asynchronously” downloads and prints HTML from list of URLs. Keeping your main process non-blocking and free to do other stuff.
$process_service = new process_service();
$url_list = array(
foreach($url_list as $url)
$html = file_get_contents($url);
echo "."; // testing
1) We instantiate our process_service
2) We have a standard array of URL’s
3) We loop over those URL’s, and call run() with 2 arguments: what you want to run in the process (anything you want from the current process in the child process must be passed with the use feature from PHP 5.3, and this works thanks to lambda serialization – credits to Jeremy Lindblom’s super_closure), and the callback for when it’s finished (arguments are whatever you returned in the first function).
4) Lastly we have a main loop which calls process_service‘s update function, which handles IO from the children processes, and such.
More found on the work page, and more to come!