Reading line by line from STDIN without blocking

女生的网名这么多〃 提交于 2019-12-07 06:33:32

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/4966365/reading-line-by-line-from-stdin-without-blocking

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!