2011-02-11 93 views
5

基本上,我正在读取STDIN的行,但我不想在等待新数据时阻塞。几乎就像使用超时流一样。从STDIN逐行读取,无阻塞

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

回答

7

想通了,用stream_set_blockingDocs来禁用阻塞。当没有输入可用时,将$line设置为false

+1

它仍然阻塞在Windows? – CMCDragonkai 2013-11-15 01:39:08