Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading line by line from STDIN without blocking

Tags:

php

stdin

Basically, I'm looking to read lines from STDIN, but I don't want to block while waiting for new data. Almost like using a stream with a timeout.

$stdin = fopen('php://stdin', 'r');

do {
  $line = fgets($stdin);

  // No input right now
  if (empty($line)) {
    // Do something before waiting for more input
  }
} while (1);
like image 980
St. John Johnson Avatar asked Feb 11 '11 06:02

St. John Johnson


1 Answers

Figured it out, use stream_set_blockingDocs to disable blocking. Sets $line to false when no input is available.

like image 54
St. John Johnson Avatar answered Nov 12 '22 16:11

St. John Johnson