从标准输入逐行读取而不阻塞

5
基本上,我想从标准输入读取行,但我不想在等待新数据时被阻塞。就像使用带有超时的流一样。
$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);
1个回答

7

已经想到了解决方案,使用stream_set_blocking文档来禁用阻塞。当没有输入可用时,将$line设置为false


1
在Windows上仍然阻塞吗? - CMCDragonkai
我知道这已经很老了,但不幸的是它在Windows上无法工作的原因在于: https://bugs.php.net/bug.php?id=34972 - Hexus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接