简体   繁体   English

从PHP脚本调用python脚本

[英]call python script from PHP script

I need to call python script from PHP script and return result back to PHP. 我需要从PHP脚本调用python脚本,然后将结果返回给PHP。 I playing with proc_open function. 我在玩proc_open函数。 But it does not work. 但这行不通。 Do you know why? 你知道为什么吗? This is PHP script: 这是PHP脚本:

$msg = "this is a new message \n bla ble !@#$%^&*%(*))(_+=-";
$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
2 => array("file","./error.log","a")
) ;
$cwd = './' ;
$command = 'python ./upper.py ';
$proc = proc_open($command, $descriptorspec, $pipes, $cwd) ;
if ( is_resource( $proc ) ) {
    fwrite( $pipes[0], $msg );
    fclose( $pipes[0] );
    fclose( $pipes[1] );
    proc_close($proc);
    echo "proc is closed\n";
}
else {
    echo 'proc is not a resource';
}

python `upper.py' script python`upper.py'脚本

import sys
print 'in python script'

data = sys.stdin.readlines()
print data

Output is : 输出为:

in php script
proc is closed

I have error in error.log: 我在error.log中有错误:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

Just in case the answer still matters to anyone: 以防万一答案仍然对任何人都重要:

The error occurs because you're closing $pipes[1] immediately, before the python script has a chance to write it, and quite likely even before it has even started running. 发生错误是因为在python脚本有机会编写$pipes[1]之前立即关闭它,而且很有可能甚至在它甚至开始运行之前就将其关闭。 The error on my system (a Mac) is: 我的系统(苹果机)上的错误是:

close failed: [Errno 32] Broken pipe

(Just out of curiosity, what type of system are you running on that's giving you that strange message?) (出于好奇,您运行的是哪种类型的系统会给您带来奇怪的信息?)

Anyway, you can avoid the error by reading the pipe before closing it, eg, 无论如何,您可以通过在关闭管道之前读取管道来避免错误,例如,

stream_get_contents($pipes[1]);

which guarantees that the python script will get a chance to write the pipe (at least once) while the pipe is still open. 这样可以确保在管道仍处于打开状态时,python脚本有机会编写管道(至少一次)。

If you are the author of the python script, there's not much point in having it write output unless your PHP script is going to read what it writes. 如果您是python脚本的作者,那么除非您的PHP脚本要读取其编写的内容,否则让它编写输出没有什么意义。

On the other hand, if the python script isn't of your making, and it writes output that you don't care about, you might be able to avoid reading the output by playing some games with pcntl_wait() or pcntl_waitpid() -- though I wouldn't bet on it. 在另一方面,如果Python脚本是你制作的不是,并把它写到你不在乎输出,你可能能够避免玩一些游戏与读取输出pcntl_wait()pcntl_waitpid() - -虽然我不敢打赌。 A safer option is probably to just read the output streams and throw them on the floor. 一个更安全的选择可能是只读取输出流并将它们放在地板上。

(Kind of raises an interesting question, whether or not you care about the output: if you don't know a priori what the end of the output will look like, and the subprocess won't exit until you close the pipes and call proc_close() , how will you know when the subprocess has actually finished doing what you called it to do?) (种类繁多的问题引发了一个有趣的问题,即您是否关心输出:如果您不知道先验 ,输出的结束会是什么样子,并且子proc_close()在关闭管道并调用proc_close()之前不会退出。 proc_close() ,您如何知道子流程实际上何时完成了您所称的工作?)

Another option (untested by me so far) might be to connect any output streams you don't care about to a descriptor like: 另一个选择(到目前为止,我还没有研究过)可能是将不需要的任何输出流连接到一个描述符,例如:

array("file", "/dev/null", "w")

or whatever the equivalent of /dev/null is on your system. 或系统上与/dev/null等价的任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM