我有一个时髦的python代码:import subprocess
value = subprocess.Popen(["php","./php/php_runner.php"],stdout=subprocess.PIPE);
问题是,我不知道如何检查php运行程序是否运行。目前,它有以下突出部分:
^{pr2}$
以及$returnValue = call_to_another_php_class();
return $returnValue;
所以我想要的是:如何使用python获得返回值(不管是什么)?
您可能会告诉我在答案中使用“PIPE”,但是(对我来说,无法理解的)python文档(http://docs.python.org/library/subprocess.html)国家:Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in >the current process, the child process may block if it generates enough output to a pipe to fill up >the OS pipe buffer.
So what do i use then, because while I don't really know what they're barking on about, i sit up and take note about notes in grey boxes. Pity they didn't spell out what i'm meant to do - but, well, what am i meant to do?
我的php代码返回的“returnValue”是python将从函数中获取的返回值?如果没有,如何返回该值?在
干杯!在
更新
多亏了给出的答案,以下是我所做的改变:编辑/etc/php5/cli/conf.d/mcrypt.ini(实际上,这只是对ubuntu10.04的一个更改,我将第一行改为以;而不是#开头。这就阻止了一个烦人的“我们不喜欢”错误不断出现)
在我的php中,我将代码改为:if (count($argv) != 4){
fwrite(STDERR, "four arguments are needed\n");
exit(1); // A response code other than 0 is a failure
}
这将把我的错误值作为一个错误。die()命令对我没用。
将python改为:value = subprocess.Popen(["php","./php/php_runner.php"],stdout=subprocess.PIPE,
stderr=subprocess.PIPE);
print value.communicate();
是的,事实上,我会做一个假设价值观。沟通()[1],因为错误就在这里。
$returnValue = call_to_another_php_class();
if ($returnValue == 1){ //hah, php has a good return value as 1.
//no problem
} else {
fwrite(STDERR,get_error_from_php_class());
exit(1);
}
布耶!在