• Eric Muyser

    "The law of attraction is a secondary law. The law of vibration is the primary law." - Bob Proctor

    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.

    3. <?php

    4. require_once("../src/process.php");

    5. $process_service = new process_service();

    6. $url_list = array(

    7. "http://google.com/",

    8. "http://facebook.com/",

    9. "http://twitter.com/"

    10. );

    11. foreach($url_list as $url)

    12. $process_service->run(

    13. function() use($url)

    14. {

    15. $html = file_get_contents($url);

    16. return $html;

    17. },

    18. function($html)

    19. {

    20. var_dump($html);

    21. });

    22. while(true)

    23. {

    24. $process_service->update();

    25. echo "."; // testing

    26. usleep(10000);

    27. }

    28. ?>


    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!