簡體   English   中英

如何從仍在PHP中運行的進程中獲得部分輸出?

[英]how do I get partial output from a process that's still running in PHP?

我有一個PHP腳本,該腳本使用proc_open()運行第二個腳本。

然后,主腳本向第二個腳本傳遞STDIN上的一些數據。

我希望第二個腳本能夠打印輸出,而主腳本可以接收輸出。

到目前為止,一切正常。

但是,我的主腳本需要多次調用第二個腳本,並且為了對此進行優化,我想保持資源開放。 也可以

但是我會逐漸看到腳本的輸出。 這似乎不起作用:看起來兩個腳本最終都在等待對方。

所以我想做的事情歸結為:

// Main script
$resource = proc_open($command, $descriptorspec, $pipes);

foreach ($data as $line) {
  fwrite($pipes[0], $line);

  // Get output back from the second script's STDOUT to see how
  // it reacted to this piece of data.
  while ($line = fgets($this->pipes[1])) {
    dump("script: $line");
  }
}

// Second script.
while ($line = trim(fgets(STDIN))) {
  print "some sort of output to report how I'm doing";
}

我想出了第二種腳本告訴主腳本停止等待的方法:

print "\\n";

然后從流程中獲取輸出時,主腳本需要執行以下操作:

// Get output back from the second script's STDOUT to see how // it reacted to this piece of data. while ($line = trim(fgets($this->pipes[1]))) { dump("script: $line"); }

第二個腳本基本上是在主腳本中使用一條空行作為消息,說“我現在正在輸出,請停止監聽”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM