44
55require_once __DIR__ . '/FunctionParser.php ' ;
66require_once __DIR__ . '/Exception/FileNotFoundException.php ' ;
7+ require_once __DIR__ . '/Exception/PHPBinNotFoundException.php ' ;
78
89use Mutilprocessing \Exception \FileNotFoundException ;
10+ use Mutilprocessing \Exception \PHPBinNotFoundException ;
911use Mutilprocessing \FunctionParser ;
1012
1113class Async
@@ -43,16 +45,16 @@ public static function discard()
4345 * @param array $args <p>
4446 * Input you want to execute the required parameters in function
4547 * </p>
48+ * @param string $phpBin You can specify the PHP path for asynchronous execution scripts.
4649 * @param array $envs
4750 * @return $this
4851 */
49- public function start ($ scriptname , $ args = [], $ envs = [])
52+ public function start ($ scriptname , $ args = [], $ phpBin = null , $ envs = [])
5053 {
5154 if ($ scriptname == 'callbackStub.php ' ) {
5255 $ cwd = __DIR__ ;
5356 } else {
5457 $ cwd = '. ' ;
55- // judge if file exists
5658 if (!is_file ($ scriptname )) {
5759 throw new FileNotFoundException ($ scriptname ." is not found " );
5860 }
@@ -62,9 +64,18 @@ public function start($scriptname, $args = [], $envs = [])
6264 } else {
6365 $ argsStr = escapeshellarg (base64_encode (json_encode ($ args )));
6466 }
67+ if ($ phpBin !== null && is_string ($ phpBin )) {
68+ // check if php-bin is validate
69+ if (!is_file ($ phpBin )) {
70+ throw new PHPBinNotFoundException ("PHP Binary : " .$ phpBin ."is not found " );
71+ }
72+ $ cmd = "{$ phpBin } {$ scriptname } ' $ argsStr' & " ;
73+ } else {
74+ $ cmd = "php $ scriptname ' $ argsStr' & " ;
75+ }
6576 $ this -> argStr = $ argsStr ;
6677 $ this ->proccess = proc_open (
67- " php $ scriptname ' $ argsStr ' & " ,
78+ $ cmd ,
6879 array (
6980 0 => array ('pipe ' , 'r ' ), //stdin (用fwrite写入数据给管道)
7081 1 => array ('pipe ' , 'w ' ), //stdout(用stream_get_contents获取管道输出)
@@ -87,11 +98,11 @@ public function start($scriptname, $args = [], $envs = [])
8798 * Input you want to execute the required parameters in function
8899 * </p>
89100 */
90- public function startFunc (callable $ function , $ args = [])
101+ public function startFunc (callable $ function , $ args = [], $ phpBin = null )
91102 {
92103 $ funcBody = FunctionParser::genTmp ($ function );
93104 $ args ['body ' ] = $ funcBody ;
94- $ this -> start ('callbackStub.php ' , $ args );
105+ $ this -> start ('callbackStub.php ' , $ args, $ phpBin );
95106 }
96107
97108 /**
0 commit comments