Multi Process In Same Time

Discussion in 'PHP' started by SNaRe, Nov 25, 2007.

  1. #1
    In one of my scripti i use processes in same time.
    For example think like downloading 10 files in same time. Think files as processes. I won't download something. I will grab daha with curl from a website.
    I checked google for this i saw curl_multi but i couldn't find good article about it .
    Is there someone that has experience about this ?
     
    SNaRe, Nov 25, 2007 IP
  2. Kwaku

    Kwaku Well-Known Member

    Messages:
    1,217
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #2
    I don't think curl_multi(_select?) has anything to do with this, but I could be wrong. However, if your PHP installation (or the one at your hoster) allows it, you should be able to use fork() or & to create processes, for instance;

    <?
    $pidlist = array();
    for ($i=0;$i<10;$i++) { // just for testing 10 'downloads'
    $pid = pcntl_fork();
    if (!$pid) {
    sleep(rand(0,5)); // just for testing, remove
    echo "Do the curl download here! $i\n";
    exit;
    } else {
    $pidlist[] = $pid;
    }
    }

    // make sure there are no zombies
    foreach($pidlist as $pid) {
    pcntl_waitpid($pid, $status);
    echo "Done\n";
    }

    echo "Do something with the downloads here\n";
    ?>

    Or, when you have an external download program;

    $some_param = "bla";
    for($i=0;$i<10;$i++)
    `my_download_script.php $some_param &`;

    Hope this helps a bit.
     
    Kwaku, Nov 25, 2007 IP