issue with stream_select() in PHP











up vote
1
down vote

favorite












I am using stream_select() but it returns 0 number of descriptors after few seconds and my function while there is still data to be read.



An unusual thing though is that if you set the time out as 0 then I always get the number of descriptors as zero.



$num = stream_select($read, $w, $e, 0);









share|improve this question
























  • Did you solve it?
    – Mattias Wadman
    Aug 4 '12 at 15:47










  • nope I could not.
    – AJ.
    Aug 7 '12 at 17:20















up vote
1
down vote

favorite












I am using stream_select() but it returns 0 number of descriptors after few seconds and my function while there is still data to be read.



An unusual thing though is that if you set the time out as 0 then I always get the number of descriptors as zero.



$num = stream_select($read, $w, $e, 0);









share|improve this question
























  • Did you solve it?
    – Mattias Wadman
    Aug 4 '12 at 15:47










  • nope I could not.
    – AJ.
    Aug 7 '12 at 17:20













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am using stream_select() but it returns 0 number of descriptors after few seconds and my function while there is still data to be read.



An unusual thing though is that if you set the time out as 0 then I always get the number of descriptors as zero.



$num = stream_select($read, $w, $e, 0);









share|improve this question















I am using stream_select() but it returns 0 number of descriptors after few seconds and my function while there is still data to be read.



An unusual thing though is that if you set the time out as 0 then I always get the number of descriptors as zero.



$num = stream_select($read, $w, $e, 0);






php timeout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 3 '12 at 16:13

























asked Aug 3 '12 at 16:00









AJ.

1,29493272




1,29493272












  • Did you solve it?
    – Mattias Wadman
    Aug 4 '12 at 15:47










  • nope I could not.
    – AJ.
    Aug 7 '12 at 17:20


















  • Did you solve it?
    – Mattias Wadman
    Aug 4 '12 at 15:47










  • nope I could not.
    – AJ.
    Aug 7 '12 at 17:20
















Did you solve it?
– Mattias Wadman
Aug 4 '12 at 15:47




Did you solve it?
– Mattias Wadman
Aug 4 '12 at 15:47












nope I could not.
– AJ.
Aug 7 '12 at 17:20




nope I could not.
– AJ.
Aug 7 '12 at 17:20












4 Answers
4






active

oldest

votes

















up vote
3
down vote













Does it return the number 0 or a FALSE boolean? FALSE means there was some error but zero could be just because of timeout or nothing interesting has happen with the streams and you should do a new select etc.



I would guess this could happen with a zero timeout as it will check and return immediately. Also if you read the PHP manual about stream-select you will see this warning about using zero timeout:




Using a timeout value of 0 allows you to instantaneously poll the status of the streams, however, it is NOT a good idea to use a 0 timeout value in a loop as it will cause your script to consume too much CPU time.




If this is a TCP stream and you want to check for connection close you should check the return value from fread etc to determine if the other peer has closed the conneciton. About the read streams array argument:




The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).







share|improve this answer




























    up vote
    2
    down vote













    stream_select() must be used in a loop



    The stream_select() function basically just polls the stream selectors you provided in the first three arguments, which means it will wait until one of the following events occur:




    • some data arrives

    • or reaches timeout (set with $tv_sec and $tv_usec) without getting any data.


    So recieving 0 as a return value is perfectly normal, it means there was no new data in the current polling cycle.



    I'd suggest to put the function in a loop something like this:



    $EOF = false;

    do {
    $tmp = null;
    $ready = stream_select($read, $write, $excl, 0, 50000);

    if ($ready === false ) {
    // something went wrong!!
    break;
    } elseif ($ready > 0) {
    foreach($read as $r) {
    $tmp .= stream_get_contents($r);
    if (feof($r)) $EOF = true;
    }

    if (!empty($tmp)) {
    //
    // DO SOMETHING WITH DATA
    //
    continue;
    }
    } else {
    // No data in the current cycle
    }
    } while(!$EOF);


    Please note that in this example, the script totally ignores everything aside from the input stream. Also, the third section of the "if" statement is for demonstration only.






    share|improve this answer






























      up vote
      0
      down vote













      http://www.php.net/stream_select




      Due to a limitation in the current Zend Engine it is not possible to
      pass a constant modifier like NULL directly as a parameter to a
      function which expects this parameter to be passed by reference.
      Instead use a temporary variable or an expression with the leftmost
      member being a temporary variable:




      <?php $e = NULL; stream_select($r, $w, $e, 0); ?>





      share|improve this answer





















      • done that still same issue.
        – AJ.
        Aug 3 '12 at 16:13










      • without any additional info, I'd suggest to run the script with strace and check what's the return value of the underlying select syscall
        – strkol
        Aug 3 '12 at 16:16


















      up vote
      0
      down vote













      I have a similar issue which is caused by the underlying socket timeout.



      Eg. I create some streams



      $streams = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);


      Then fork, and use a block such as the following



        stream_set_blocking($pipes[1], 1);
      stream_set_blocking($pipes[2], 1);
      $pipesToRead = array($pipes[1], $pipes[2]);

      while (!feof($pipesToRead[0]) || !feof($pipesToRead[1])) {
      $reads = $pipesToRead;
      $writes = null;
      $excepts = $pipesToRead;
      $tSec = null;
      stream_select($reads, $writes, $excepts, $tSec);
      // while it's generating any kind of output, duplicate it wherever it
      // needs to go
      foreach ($reads as &$read) {
      $chunk = fread($read, 8192);
      foreach ($streams as &$stream)
      fwrite($stream, $chunk);
      }
      }


      Glossing over what other things might be wrong there, my $tSec argument to stream_select is ignored, and the "stream" will timeout after 60 seconds of inactivity and produce an EOF.



      If I add the following after creating the streams



      stream_set_timeout($streams[0], 999);
      stream_set_timeout($streams[1], 999);


      Then I get the result I desire, even if there's no activity on the underlying stream for longer than 60 seconds.



      I feel that this might be a bug, because I don't want that EOF after 60 seconds of inactivity on the underlying stream, and I don't want to plug in some arbitrarily large value to avoid hitting the timeout if my processes are idle for some time.



      In addition, even if the 60 second timeout remains, I think it should just timeout on my stream_select() call and my loop should be able to continue.






      share|improve this answer





















      • Further reading of the tv_sec argument to stream_select (php.net/manual/en/function.stream-select.php) shows that when it's "null", the only way for the function to return is for an event to happen on one the streams. If PHP isn't going to hang indefinitely, this means the timeout value needs to be one such event, so I should not be surprised by this behaviour. I will try passing a value in tv_sec to see if it means I can remove the call to stream_set_timeout...
        – Andrew Sharpe
        Dec 3 at 1:23











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11799252%2fissue-with-stream-select-in-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Does it return the number 0 or a FALSE boolean? FALSE means there was some error but zero could be just because of timeout or nothing interesting has happen with the streams and you should do a new select etc.



      I would guess this could happen with a zero timeout as it will check and return immediately. Also if you read the PHP manual about stream-select you will see this warning about using zero timeout:




      Using a timeout value of 0 allows you to instantaneously poll the status of the streams, however, it is NOT a good idea to use a 0 timeout value in a loop as it will cause your script to consume too much CPU time.




      If this is a TCP stream and you want to check for connection close you should check the return value from fread etc to determine if the other peer has closed the conneciton. About the read streams array argument:




      The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).







      share|improve this answer

























        up vote
        3
        down vote













        Does it return the number 0 or a FALSE boolean? FALSE means there was some error but zero could be just because of timeout or nothing interesting has happen with the streams and you should do a new select etc.



        I would guess this could happen with a zero timeout as it will check and return immediately. Also if you read the PHP manual about stream-select you will see this warning about using zero timeout:




        Using a timeout value of 0 allows you to instantaneously poll the status of the streams, however, it is NOT a good idea to use a 0 timeout value in a loop as it will cause your script to consume too much CPU time.




        If this is a TCP stream and you want to check for connection close you should check the return value from fread etc to determine if the other peer has closed the conneciton. About the read streams array argument:




        The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).







        share|improve this answer























          up vote
          3
          down vote










          up vote
          3
          down vote









          Does it return the number 0 or a FALSE boolean? FALSE means there was some error but zero could be just because of timeout or nothing interesting has happen with the streams and you should do a new select etc.



          I would guess this could happen with a zero timeout as it will check and return immediately. Also if you read the PHP manual about stream-select you will see this warning about using zero timeout:




          Using a timeout value of 0 allows you to instantaneously poll the status of the streams, however, it is NOT a good idea to use a 0 timeout value in a loop as it will cause your script to consume too much CPU time.




          If this is a TCP stream and you want to check for connection close you should check the return value from fread etc to determine if the other peer has closed the conneciton. About the read streams array argument:




          The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).







          share|improve this answer












          Does it return the number 0 or a FALSE boolean? FALSE means there was some error but zero could be just because of timeout or nothing interesting has happen with the streams and you should do a new select etc.



          I would guess this could happen with a zero timeout as it will check and return immediately. Also if you read the PHP manual about stream-select you will see this warning about using zero timeout:




          Using a timeout value of 0 allows you to instantaneously poll the status of the streams, however, it is NOT a good idea to use a 0 timeout value in a loop as it will cause your script to consume too much CPU time.




          If this is a TCP stream and you want to check for connection close you should check the return value from fread etc to determine if the other peer has closed the conneciton. About the read streams array argument:




          The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 3 '12 at 17:53









          Mattias Wadman

          8,82423449




          8,82423449
























              up vote
              2
              down vote













              stream_select() must be used in a loop



              The stream_select() function basically just polls the stream selectors you provided in the first three arguments, which means it will wait until one of the following events occur:




              • some data arrives

              • or reaches timeout (set with $tv_sec and $tv_usec) without getting any data.


              So recieving 0 as a return value is perfectly normal, it means there was no new data in the current polling cycle.



              I'd suggest to put the function in a loop something like this:



              $EOF = false;

              do {
              $tmp = null;
              $ready = stream_select($read, $write, $excl, 0, 50000);

              if ($ready === false ) {
              // something went wrong!!
              break;
              } elseif ($ready > 0) {
              foreach($read as $r) {
              $tmp .= stream_get_contents($r);
              if (feof($r)) $EOF = true;
              }

              if (!empty($tmp)) {
              //
              // DO SOMETHING WITH DATA
              //
              continue;
              }
              } else {
              // No data in the current cycle
              }
              } while(!$EOF);


              Please note that in this example, the script totally ignores everything aside from the input stream. Also, the third section of the "if" statement is for demonstration only.






              share|improve this answer



























                up vote
                2
                down vote













                stream_select() must be used in a loop



                The stream_select() function basically just polls the stream selectors you provided in the first three arguments, which means it will wait until one of the following events occur:




                • some data arrives

                • or reaches timeout (set with $tv_sec and $tv_usec) without getting any data.


                So recieving 0 as a return value is perfectly normal, it means there was no new data in the current polling cycle.



                I'd suggest to put the function in a loop something like this:



                $EOF = false;

                do {
                $tmp = null;
                $ready = stream_select($read, $write, $excl, 0, 50000);

                if ($ready === false ) {
                // something went wrong!!
                break;
                } elseif ($ready > 0) {
                foreach($read as $r) {
                $tmp .= stream_get_contents($r);
                if (feof($r)) $EOF = true;
                }

                if (!empty($tmp)) {
                //
                // DO SOMETHING WITH DATA
                //
                continue;
                }
                } else {
                // No data in the current cycle
                }
                } while(!$EOF);


                Please note that in this example, the script totally ignores everything aside from the input stream. Also, the third section of the "if" statement is for demonstration only.






                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  stream_select() must be used in a loop



                  The stream_select() function basically just polls the stream selectors you provided in the first three arguments, which means it will wait until one of the following events occur:




                  • some data arrives

                  • or reaches timeout (set with $tv_sec and $tv_usec) without getting any data.


                  So recieving 0 as a return value is perfectly normal, it means there was no new data in the current polling cycle.



                  I'd suggest to put the function in a loop something like this:



                  $EOF = false;

                  do {
                  $tmp = null;
                  $ready = stream_select($read, $write, $excl, 0, 50000);

                  if ($ready === false ) {
                  // something went wrong!!
                  break;
                  } elseif ($ready > 0) {
                  foreach($read as $r) {
                  $tmp .= stream_get_contents($r);
                  if (feof($r)) $EOF = true;
                  }

                  if (!empty($tmp)) {
                  //
                  // DO SOMETHING WITH DATA
                  //
                  continue;
                  }
                  } else {
                  // No data in the current cycle
                  }
                  } while(!$EOF);


                  Please note that in this example, the script totally ignores everything aside from the input stream. Also, the third section of the "if" statement is for demonstration only.






                  share|improve this answer














                  stream_select() must be used in a loop



                  The stream_select() function basically just polls the stream selectors you provided in the first three arguments, which means it will wait until one of the following events occur:




                  • some data arrives

                  • or reaches timeout (set with $tv_sec and $tv_usec) without getting any data.


                  So recieving 0 as a return value is perfectly normal, it means there was no new data in the current polling cycle.



                  I'd suggest to put the function in a loop something like this:



                  $EOF = false;

                  do {
                  $tmp = null;
                  $ready = stream_select($read, $write, $excl, 0, 50000);

                  if ($ready === false ) {
                  // something went wrong!!
                  break;
                  } elseif ($ready > 0) {
                  foreach($read as $r) {
                  $tmp .= stream_get_contents($r);
                  if (feof($r)) $EOF = true;
                  }

                  if (!empty($tmp)) {
                  //
                  // DO SOMETHING WITH DATA
                  //
                  continue;
                  }
                  } else {
                  // No data in the current cycle
                  }
                  } while(!$EOF);


                  Please note that in this example, the script totally ignores everything aside from the input stream. Also, the third section of the "if" statement is for demonstration only.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 27 '17 at 8:09

























                  answered May 30 '16 at 13:18









                  Gergely Lukacsy

                  1,0871121




                  1,0871121






















                      up vote
                      0
                      down vote













                      http://www.php.net/stream_select




                      Due to a limitation in the current Zend Engine it is not possible to
                      pass a constant modifier like NULL directly as a parameter to a
                      function which expects this parameter to be passed by reference.
                      Instead use a temporary variable or an expression with the leftmost
                      member being a temporary variable:




                      <?php $e = NULL; stream_select($r, $w, $e, 0); ?>





                      share|improve this answer





















                      • done that still same issue.
                        – AJ.
                        Aug 3 '12 at 16:13










                      • without any additional info, I'd suggest to run the script with strace and check what's the return value of the underlying select syscall
                        – strkol
                        Aug 3 '12 at 16:16















                      up vote
                      0
                      down vote













                      http://www.php.net/stream_select




                      Due to a limitation in the current Zend Engine it is not possible to
                      pass a constant modifier like NULL directly as a parameter to a
                      function which expects this parameter to be passed by reference.
                      Instead use a temporary variable or an expression with the leftmost
                      member being a temporary variable:




                      <?php $e = NULL; stream_select($r, $w, $e, 0); ?>





                      share|improve this answer





















                      • done that still same issue.
                        – AJ.
                        Aug 3 '12 at 16:13










                      • without any additional info, I'd suggest to run the script with strace and check what's the return value of the underlying select syscall
                        – strkol
                        Aug 3 '12 at 16:16













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      http://www.php.net/stream_select




                      Due to a limitation in the current Zend Engine it is not possible to
                      pass a constant modifier like NULL directly as a parameter to a
                      function which expects this parameter to be passed by reference.
                      Instead use a temporary variable or an expression with the leftmost
                      member being a temporary variable:




                      <?php $e = NULL; stream_select($r, $w, $e, 0); ?>





                      share|improve this answer












                      http://www.php.net/stream_select




                      Due to a limitation in the current Zend Engine it is not possible to
                      pass a constant modifier like NULL directly as a parameter to a
                      function which expects this parameter to be passed by reference.
                      Instead use a temporary variable or an expression with the leftmost
                      member being a temporary variable:




                      <?php $e = NULL; stream_select($r, $w, $e, 0); ?>






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 3 '12 at 16:08









                      strkol

                      1,4441111




                      1,4441111












                      • done that still same issue.
                        – AJ.
                        Aug 3 '12 at 16:13










                      • without any additional info, I'd suggest to run the script with strace and check what's the return value of the underlying select syscall
                        – strkol
                        Aug 3 '12 at 16:16


















                      • done that still same issue.
                        – AJ.
                        Aug 3 '12 at 16:13










                      • without any additional info, I'd suggest to run the script with strace and check what's the return value of the underlying select syscall
                        – strkol
                        Aug 3 '12 at 16:16
















                      done that still same issue.
                      – AJ.
                      Aug 3 '12 at 16:13




                      done that still same issue.
                      – AJ.
                      Aug 3 '12 at 16:13












                      without any additional info, I'd suggest to run the script with strace and check what's the return value of the underlying select syscall
                      – strkol
                      Aug 3 '12 at 16:16




                      without any additional info, I'd suggest to run the script with strace and check what's the return value of the underlying select syscall
                      – strkol
                      Aug 3 '12 at 16:16










                      up vote
                      0
                      down vote













                      I have a similar issue which is caused by the underlying socket timeout.



                      Eg. I create some streams



                      $streams = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);


                      Then fork, and use a block such as the following



                        stream_set_blocking($pipes[1], 1);
                      stream_set_blocking($pipes[2], 1);
                      $pipesToRead = array($pipes[1], $pipes[2]);

                      while (!feof($pipesToRead[0]) || !feof($pipesToRead[1])) {
                      $reads = $pipesToRead;
                      $writes = null;
                      $excepts = $pipesToRead;
                      $tSec = null;
                      stream_select($reads, $writes, $excepts, $tSec);
                      // while it's generating any kind of output, duplicate it wherever it
                      // needs to go
                      foreach ($reads as &$read) {
                      $chunk = fread($read, 8192);
                      foreach ($streams as &$stream)
                      fwrite($stream, $chunk);
                      }
                      }


                      Glossing over what other things might be wrong there, my $tSec argument to stream_select is ignored, and the "stream" will timeout after 60 seconds of inactivity and produce an EOF.



                      If I add the following after creating the streams



                      stream_set_timeout($streams[0], 999);
                      stream_set_timeout($streams[1], 999);


                      Then I get the result I desire, even if there's no activity on the underlying stream for longer than 60 seconds.



                      I feel that this might be a bug, because I don't want that EOF after 60 seconds of inactivity on the underlying stream, and I don't want to plug in some arbitrarily large value to avoid hitting the timeout if my processes are idle for some time.



                      In addition, even if the 60 second timeout remains, I think it should just timeout on my stream_select() call and my loop should be able to continue.






                      share|improve this answer





















                      • Further reading of the tv_sec argument to stream_select (php.net/manual/en/function.stream-select.php) shows that when it's "null", the only way for the function to return is for an event to happen on one the streams. If PHP isn't going to hang indefinitely, this means the timeout value needs to be one such event, so I should not be surprised by this behaviour. I will try passing a value in tv_sec to see if it means I can remove the call to stream_set_timeout...
                        – Andrew Sharpe
                        Dec 3 at 1:23















                      up vote
                      0
                      down vote













                      I have a similar issue which is caused by the underlying socket timeout.



                      Eg. I create some streams



                      $streams = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);


                      Then fork, and use a block such as the following



                        stream_set_blocking($pipes[1], 1);
                      stream_set_blocking($pipes[2], 1);
                      $pipesToRead = array($pipes[1], $pipes[2]);

                      while (!feof($pipesToRead[0]) || !feof($pipesToRead[1])) {
                      $reads = $pipesToRead;
                      $writes = null;
                      $excepts = $pipesToRead;
                      $tSec = null;
                      stream_select($reads, $writes, $excepts, $tSec);
                      // while it's generating any kind of output, duplicate it wherever it
                      // needs to go
                      foreach ($reads as &$read) {
                      $chunk = fread($read, 8192);
                      foreach ($streams as &$stream)
                      fwrite($stream, $chunk);
                      }
                      }


                      Glossing over what other things might be wrong there, my $tSec argument to stream_select is ignored, and the "stream" will timeout after 60 seconds of inactivity and produce an EOF.



                      If I add the following after creating the streams



                      stream_set_timeout($streams[0], 999);
                      stream_set_timeout($streams[1], 999);


                      Then I get the result I desire, even if there's no activity on the underlying stream for longer than 60 seconds.



                      I feel that this might be a bug, because I don't want that EOF after 60 seconds of inactivity on the underlying stream, and I don't want to plug in some arbitrarily large value to avoid hitting the timeout if my processes are idle for some time.



                      In addition, even if the 60 second timeout remains, I think it should just timeout on my stream_select() call and my loop should be able to continue.






                      share|improve this answer





















                      • Further reading of the tv_sec argument to stream_select (php.net/manual/en/function.stream-select.php) shows that when it's "null", the only way for the function to return is for an event to happen on one the streams. If PHP isn't going to hang indefinitely, this means the timeout value needs to be one such event, so I should not be surprised by this behaviour. I will try passing a value in tv_sec to see if it means I can remove the call to stream_set_timeout...
                        – Andrew Sharpe
                        Dec 3 at 1:23













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      I have a similar issue which is caused by the underlying socket timeout.



                      Eg. I create some streams



                      $streams = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);


                      Then fork, and use a block such as the following



                        stream_set_blocking($pipes[1], 1);
                      stream_set_blocking($pipes[2], 1);
                      $pipesToRead = array($pipes[1], $pipes[2]);

                      while (!feof($pipesToRead[0]) || !feof($pipesToRead[1])) {
                      $reads = $pipesToRead;
                      $writes = null;
                      $excepts = $pipesToRead;
                      $tSec = null;
                      stream_select($reads, $writes, $excepts, $tSec);
                      // while it's generating any kind of output, duplicate it wherever it
                      // needs to go
                      foreach ($reads as &$read) {
                      $chunk = fread($read, 8192);
                      foreach ($streams as &$stream)
                      fwrite($stream, $chunk);
                      }
                      }


                      Glossing over what other things might be wrong there, my $tSec argument to stream_select is ignored, and the "stream" will timeout after 60 seconds of inactivity and produce an EOF.



                      If I add the following after creating the streams



                      stream_set_timeout($streams[0], 999);
                      stream_set_timeout($streams[1], 999);


                      Then I get the result I desire, even if there's no activity on the underlying stream for longer than 60 seconds.



                      I feel that this might be a bug, because I don't want that EOF after 60 seconds of inactivity on the underlying stream, and I don't want to plug in some arbitrarily large value to avoid hitting the timeout if my processes are idle for some time.



                      In addition, even if the 60 second timeout remains, I think it should just timeout on my stream_select() call and my loop should be able to continue.






                      share|improve this answer












                      I have a similar issue which is caused by the underlying socket timeout.



                      Eg. I create some streams



                      $streams = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);


                      Then fork, and use a block such as the following



                        stream_set_blocking($pipes[1], 1);
                      stream_set_blocking($pipes[2], 1);
                      $pipesToRead = array($pipes[1], $pipes[2]);

                      while (!feof($pipesToRead[0]) || !feof($pipesToRead[1])) {
                      $reads = $pipesToRead;
                      $writes = null;
                      $excepts = $pipesToRead;
                      $tSec = null;
                      stream_select($reads, $writes, $excepts, $tSec);
                      // while it's generating any kind of output, duplicate it wherever it
                      // needs to go
                      foreach ($reads as &$read) {
                      $chunk = fread($read, 8192);
                      foreach ($streams as &$stream)
                      fwrite($stream, $chunk);
                      }
                      }


                      Glossing over what other things might be wrong there, my $tSec argument to stream_select is ignored, and the "stream" will timeout after 60 seconds of inactivity and produce an EOF.



                      If I add the following after creating the streams



                      stream_set_timeout($streams[0], 999);
                      stream_set_timeout($streams[1], 999);


                      Then I get the result I desire, even if there's no activity on the underlying stream for longer than 60 seconds.



                      I feel that this might be a bug, because I don't want that EOF after 60 seconds of inactivity on the underlying stream, and I don't want to plug in some arbitrarily large value to avoid hitting the timeout if my processes are idle for some time.



                      In addition, even if the 60 second timeout remains, I think it should just timeout on my stream_select() call and my loop should be able to continue.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 20 at 2:53









                      Andrew Sharpe

                      132




                      132












                      • Further reading of the tv_sec argument to stream_select (php.net/manual/en/function.stream-select.php) shows that when it's "null", the only way for the function to return is for an event to happen on one the streams. If PHP isn't going to hang indefinitely, this means the timeout value needs to be one such event, so I should not be surprised by this behaviour. I will try passing a value in tv_sec to see if it means I can remove the call to stream_set_timeout...
                        – Andrew Sharpe
                        Dec 3 at 1:23


















                      • Further reading of the tv_sec argument to stream_select (php.net/manual/en/function.stream-select.php) shows that when it's "null", the only way for the function to return is for an event to happen on one the streams. If PHP isn't going to hang indefinitely, this means the timeout value needs to be one such event, so I should not be surprised by this behaviour. I will try passing a value in tv_sec to see if it means I can remove the call to stream_set_timeout...
                        – Andrew Sharpe
                        Dec 3 at 1:23
















                      Further reading of the tv_sec argument to stream_select (php.net/manual/en/function.stream-select.php) shows that when it's "null", the only way for the function to return is for an event to happen on one the streams. If PHP isn't going to hang indefinitely, this means the timeout value needs to be one such event, so I should not be surprised by this behaviour. I will try passing a value in tv_sec to see if it means I can remove the call to stream_set_timeout...
                      – Andrew Sharpe
                      Dec 3 at 1:23




                      Further reading of the tv_sec argument to stream_select (php.net/manual/en/function.stream-select.php) shows that when it's "null", the only way for the function to return is for an event to happen on one the streams. If PHP isn't going to hang indefinitely, this means the timeout value needs to be one such event, so I should not be surprised by this behaviour. I will try passing a value in tv_sec to see if it means I can remove the call to stream_set_timeout...
                      – Andrew Sharpe
                      Dec 3 at 1:23


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11799252%2fissue-with-stream-select-in-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin