本文介绍了没有得到Popen的完整回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用popen运行进程;-

Hi I'm running a process with popen;-

$handle = popen('python scriptos.py', "r");
while (!feof($handle)) {
    $data = fgets($handle);
    echo "> ".$data;
}

我从返回5行的进程中只得到3行.我在CLi中运行此确切命令,我将获得更多响应.好像它早点停止阅读了(它可能需要一些时间才能完成,并且在工作时更新接下来的2行,这是一个进度指示器).

And I'm only getting 3 lines from a process that returns 5 lines. I run this exact command in CLi and I will get more response. It's as if it stops reading early (it can take time to complete and updates the next 2 lines whilst working, it's a progress indicator).

我做错什么了吗? proc_open 是否更合适(我开始看看我是否可以尝试).

Am I doing anything wrong? Is proc_open more suitable (i've started seeing if I can try that).

推荐答案

丢失的两行可能正在写入,而popen()仅返回STDOUT的指针.

The two missing lines are probably being written to STDERR, and popen() only returns a pointer for STDOUT.

您可以使用 proc_open() 获取STDERR的指针,或更改popen()线到

$handle = popen('python scriptos.py 2>&1', "r");

将STDERR重定向到STDOUT,因此它们包含在您的输出中.

to redirect STDERR to STDOUT, so they are included in your output.

这篇关于没有得到Popen的完整回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 14:43