Skip to content

Commit 33f1916

Browse files
author
郭庆哲
committed
+add phpBin to set a specific php version when run the script or callback
=update readme
1 parent 52b0b49 commit 33f1916

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
- can get callable function return value
2222

23+
- You can specify the PHP path for asynchronous execution scripts by specifying the phpBin parameter.
24+
25+
- Exception Handling
26+
2327
## Installation
2428
You can use composer to install this library from the command line.
2529

@@ -36,7 +40,7 @@ composer require sinacms/multiprocess
3640

3741
use Mutilprocessing\Async;
3842

39-
Async::create()->run('task.php', ['runTest'.$i]);
43+
Async::create()->start('task.php', ['runTest'.$i]);
4044
```
4145

4246
### distribute tasks by a simple function and async execute
@@ -50,7 +54,7 @@ use Mutilprocessing\Async;
5054

5155
Async::create()->startFunc(function($param1, $param2) {
5256
echo $param1.$param2.PHP_EOL;
53-
}, ['param1' => 'hello', 'param2' => ' world'])
57+
}, ['param1' => 'hello', 'param2' => ' world']);
5458

5559
$func = function ($param1, $param2) {
5660
echo "this is an another func";
@@ -147,8 +151,8 @@ Async::discard();
147151
* ### Async
148152
* ### option shorthand
149153
* public static function create()
150-
* public static function start($scriptname, $args, $envs = [])
151-
* public function startFunc(callable $function, $args = [])
154+
* public static function start($scriptname, $args, $phpBin="", $envs = [])
155+
* public function startFunc(callable $function, $args = [], $phpBin="")
152156
* public static function discard()
153157
* public static function wait(callable $logHandler = null)
154158
* public static function getArgs($argv = null)
@@ -158,7 +162,7 @@ Async::discard();
158162
* public static function genTmp(callable $function)
159163

160164

161-
## Plan
165+
## Todo
162166
* regCallback for each execution (on process)
163167
* add multi execution unit and start once
164168

src/Async.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
require_once __DIR__ . '/FunctionParser.php';
66
require_once __DIR__ . '/Exception/FileNotFoundException.php';
7+
require_once __DIR__ . '/Exception/PHPBinNotFoundException.php';
78

89
use Mutilprocessing\Exception\FileNotFoundException;
10+
use Mutilprocessing\Exception\PHPBinNotFoundException;
911
use Mutilprocessing\FunctionParser;
1012

1113
class 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
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: shixi_qingzhe
5+
* Date: 18/8/10
6+
* Time: 下午3:41
7+
*/
8+
9+
namespace Mutilprocessing\Exception;
10+
11+
12+
class PHPBinNotFoundException extends \Exception
13+
{
14+
}

0 commit comments

Comments
 (0)