Reading line by line from STDIN without blocking

2019-04-29 13:28发布

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);

标签: php stdin
1条回答
Rolldiameter
2楼-- · 2019-04-29 13:54

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

查看更多
登录 后发表回答