• Home
  • About
  • Who Am I?
  •  

    Multi Threading with PHP?

    Ok the title is a bit mis-leading , but well there was a point where i had to run alot of PHP processes simultaneously and use the servers resources to the fullest. For instance if you are making a cralwer or something and find that its too slow processing a page and then moving on to the next. You can use this approach.

    for($i = 1 ; $i < 10 ; $i++){
    $param1 = “anything”;
    $param2 = $i;

    exec(”pkill -f ‘runone.php $param1 $param2′”);
    exec(”/usr/local/bin/php /home/abc/public_html/runone.php $param1 $param2 > /dev/null &”);
    }

    What this will do is , first kill the process if it already exists in the background and then tell linux to execute PHP from command prompt with the script path as parameter , then $param1 and $param2 can be the parameters you pass to that script , because remember that PHP from shell CAN NOT take parameters from query string.

    So $param1 and $param2 will be received in runone.php like this..
    $param1 = $argv[1];
    $param2 = $argv[2];

    Hope this helps someone! :)


    One Response to “Multi Threading with PHP?”

    1. linux user Says:

      I currently doing threading using PHP.
      Thanks, this is useful.

    Leave a Reply