2010-10-30 42 views
0

我正在尝试使用proc_open(需要双向支持)调用C++/python文件。Proc_open()C++/python

做一些在线的研究,我发现创建此代码后:(我先用C++试了一下,失败后我试图蟒蛇一样)

PHP:

<?php 
$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("file", "error-output.txt", "a") 
); 

$process = proc_open('new.exe', $descriptorspec, $pipes); 

$input = 20; 
$exp = 2; 
if (is_resource($process)) { 
    print fgets($pipes[1]); 
    fwrite($pipes[0], $input); 

    print fgets($pipes[1]); 
    fwrite($pipes[0], $exp); 

    print fgets($pipes[1]); 

    fclose($pipes[1]); 
    fclose($pipes[0]); 
    $return_value = proc_close($process); 

    echo "command returned $return_value\n"; 
} else { 
    echo "No resource availeble"; 
} 
?> 

C++:

#include <iostream> 
using namespace std; 

int main() { 
    // Get variables from php 
    cout << "input" << endl; 
    cin << input 
    cout << "exponent" << endl; 
    cin << exp 

    // Process variables 
    int answer = input + exp; 

    // Return variables 
    cout << answer; 

    // End c++ script 
    return 0; 
} 

的Python:

print 'enter input' 
inp = raw_input() 

print 'enter exponent' 
exp = raw_input() 

ant = inp + exp 

print ant 

但遗憾的是,它仍然以相同的错误失败:文件不被识别为内部或外部命令,程序或批处理文件。

一些额外的信息:

我使用WAMP与PHP 5.3.0 返回值,我从proc_close() = 1

+1

你编译C++程序? – 2010-10-30 14:32:21

回答

0

你误会什么proc_open()确实得到。它像打开命令行一样打开一个进程,例如(在Unix中)diff file1.txt file2.txtproc_open()不运行可执行文件。

根据页面我认为你从http://php.net/manual/en/function.proc-open.php得到了你的代码来执行一个外部程序,你可以使用exec()。你可以使用这个(提供的可执行文件是在正确的目录):

<?php 
echo exec('someprogram.exe'); 
?> 

注:我不能肯定,这将有一个Windows可执行文件的工作。

这是假设someprogram.exe是一个编译的可执行文件,由您发布的C++源代码构成。如果您想从PHP运行Python程序,首先使用Windows祝您好运,但您希望使用proc_open()拨打python somescript.py,就像您从命令行执行的操作一样。

0

指定可执行文件的绝对路径:

$process = proc_open('/path/to/new.exe', $descriptorspec, $pipes); 

我可以让PHP通过通过命令行调用下面的PHP脚本,以交谈的Perl与proc_open:

#!/usr/bin/php -q 
$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("file", "error.log", "a") 
); 

$process = proc_open('/path/to/procopen.pl ' . $argv[1], 
    $descriptorspec, $pipes); 

if (is_resource($process)) { 

    // print pipe output 
    echo stream_get_contents($pipes[1]); 

    // close pipe 
    fclose($pipes[1]); 

    // close process 
    proc_close($process); 
} 

这里是我的Perl脚本与上面的PHP脚本位于同一目录中:

#!/usr/bin/perl 
print @ARGV; 

但是我无法获取调用你的Python脚本时运行PHP。命令行只是永远停止。为什么是这样?

1

你的代码有两个问题(至少对于运行你的Python脚本)。首先,你不会将你的Python输出刷新到STDOUT,所以它永远不会到达PHP。这导致fgetc()无限地阻止。这是一个简单的修复,只需添加一些flush()电话:

#!/usr/bin/env python 
import sys 

print 'enter input' 
sys.stdout.flush() 
inp = raw_input() 

print 'enter exponent' 
sys.stdout.flush() 
exp = raw_input() 

ant = inp + exp 

print ant 
sys.stdout.flush() 

然后,在你的PHP代码你是不是当你写的Python脚本STDIN发送任何换行符。所以,Python的raw_input()无限期地等待换行符。再次,一个简单的修复。只需添加"\n"fwrite()电话:

#!/usr/bin/php 
<?php 
$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("file", "error-output.txt", "a") 
); 

$process = proc_open('/path/to/python/script.py', $descriptorspec, $pipes); 

$input = 20; 
$exp = 2; 
if (is_resource($process)) { 
    print fgets($pipes[1]); 
    fwrite($pipes[0], $input . "\n"); 

    print fgets($pipes[1]); 
    fwrite($pipes[0], $exp . "\n"); 

    print fgets($pipes[1]); 

    fclose($pipes[1]); 
    fclose($pipes[0]); 
    $return_value = proc_close($process); 

    echo "command returned $return_value\n"; 
} else { 
    echo "No resource availeble"; 
} 

而这些变化,我可以让你的代码按预期方式工作:

$ ./procopen.php 
enter input 
enter exponent 
202 
command returned 0