2010-10-30 44 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