Page 1 of 1

Stream_select is buggy ;)

Posted: Thu May 29, 2008 6:39 am
by Fredde
I am trying to request headers from several external web servers in parallel. I'm using the stream_select example on IBM (http://www.ibm.com/developerworks/web/l ... multitask/) but I can't get stream_select to work. It seems it never notices any changes in the $read array and hence time-out.

I'm using PHP 5.2.5 and Mac OS X 10.5.2. Any guidance on this would be extremely helpful.

Code: Select all

 
  <?php
    echo "Program starts at ". date('h:i:s') . ".\n";
 
        $timeout=10; 
        $result=array(); 
        $sockets=array(); 
        $convenient_read_block=8192;
        
        /* Issue all requests simultaneously; there's no blocking. */
        $delay=15;
        $id=0;
        while ($delay > 0) {
            $s=stream_socket_client("phaseit.net:80", $errno,
                  $errstr, $timeout,
                  STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); 
            if ($s) { 
                $sockets[$id++]=$s; 
                $http_message="GET /demonstration/delay?delay=" .
                    $delay . " HTTP/1.0\r\nHost: phaseit.net\r\n\r\n"; 
                fwrite($s, $http_message);
            } else { 
                echo "Stream " . $id . " failed to open correctly.";
            } 
            $delay -= 3;
        } 
        
        while (count($sockets)) { 
            $read=$sockets; 
            stream_select($read, $w=null, $e=null, $timeout); 
            if (count($read)) {
                /* stream_select generally shuffles $read, so we need to
                   compute from which socket(s) we're reading. */
                foreach ($read as $r) { 
                    $id=array_search($r, $sockets); 
                    $data=fread($r, $convenient_read_block); 
                    /* A socket is readable either because it has
                       data to read, OR because it's at EOF. */
                    if (strlen($data) == 0) { 
                        echo "Stream " . $id . " closes at " . date('h:i:s') . ".\n";
                        fclose($r); 
                        unset($sockets[$id]); 
                    } else { 
                        $result[$id] .= $data; 
                    } 
                } 
            } else { 
                /* A time-out means that *all* streams have failed
                   to receive a response. */
                echo "Time-out!\n";
                break;
            } 
        } 
       ?>
 

Re: Stream_select is buggy ;)

Posted: Fri May 30, 2008 9:46 am
by dml
First of all, your php script is making a http request to a url at phaseit.net. Can you confirm that your php script can access that url? Do something like a file_get_contents on http://phaseit.net/demonstration/delay

The timeout is ten seconds, so it could be the phaseit server or the network having a bad day. If you know how to do it, it would eliminate a lot of uncertainty if you were able to test entirely on one machine rather than having to go across the Internet. Phaseit's script is basically printing something out, going to sleep for a few seconds, and printing something else out. Could you implement a script like that on your own server for local testing?