Skip to content

Commit

Permalink
+add phpBin to set a specific php version when run the script or call…
Browse files Browse the repository at this point in the history
…back

=update readme
  • Loading branch information
郭庆哲 committed Aug 10, 2018
1 parent 52b0b49 commit 33f1916
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

- can get callable function return value

- You can specify the PHP path for asynchronous execution scripts by specifying the phpBin parameter.

- Exception Handling

## Installation
You can use composer to install this library from the command line.

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

use Mutilprocessing\Async;

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

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

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

$func = function ($param1, $param2) {
echo "this is an another func";
Expand Down Expand Up @@ -147,8 +151,8 @@ Async::discard();
* ### Async
* ### option shorthand
* public static function create()
* public static function start($scriptname, $args, $envs = [])
* public function startFunc(callable $function, $args = [])
* public static function start($scriptname, $args, $phpBin="", $envs = [])
* public function startFunc(callable $function, $args = [], $phpBin="")
* public static function discard()
* public static function wait(callable $logHandler = null)
* public static function getArgs($argv = null)
Expand All @@ -158,7 +162,7 @@ Async::discard();
* public static function genTmp(callable $function)


## Plan
## Todo
* regCallback for each execution (on process)
* add multi execution unit and start once

Expand Down
21 changes: 16 additions & 5 deletions src/Async.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

require_once __DIR__ . '/FunctionParser.php';
require_once __DIR__ . '/Exception/FileNotFoundException.php';
require_once __DIR__ . '/Exception/PHPBinNotFoundException.php';

use Mutilprocessing\Exception\FileNotFoundException;
use Mutilprocessing\Exception\PHPBinNotFoundException;
use Mutilprocessing\FunctionParser;

class Async
Expand Down Expand Up @@ -43,16 +45,16 @@ public static function discard()
* @param array $args <p>
* Input you want to execute the required parameters in function
* </p>
* @param string $phpBin You can specify the PHP path for asynchronous execution scripts.
* @param array $envs
* @return $this
*/
public function start($scriptname, $args = [], $envs = [])
public function start($scriptname, $args = [], $phpBin = null, $envs = [])
{
if ($scriptname == 'callbackStub.php') {
$cwd = __DIR__;
} else {
$cwd = '.';
// judge if file exists
if (!is_file($scriptname)) {
throw new FileNotFoundException($scriptname." is not found");
}
Expand All @@ -62,9 +64,18 @@ public function start($scriptname, $args = [], $envs = [])
} else {
$argsStr = escapeshellarg(base64_encode(json_encode($args)));
}
if ($phpBin !== null && is_string($phpBin)) {
// check if php-bin is validate
if (!is_file($phpBin)) {
throw new PHPBinNotFoundException("PHP Binary :".$phpBin."is not found");
}
$cmd = "{$phpBin} {$scriptname} '$argsStr' & ";
} else {
$cmd = "php $scriptname '$argsStr' & ";
}
$this -> argStr = $argsStr;
$this->proccess = proc_open(
"php $scriptname '$argsStr' & ",
$cmd,
array(
0 => array('pipe', 'r'), //stdin (用fwrite写入数据给管道)
1 => array('pipe', 'w'), //stdout(用stream_get_contents获取管道输出)
Expand All @@ -87,11 +98,11 @@ public function start($scriptname, $args = [], $envs = [])
* Input you want to execute the required parameters in function
* </p>
*/
public function startFunc(callable $function, $args = [])
public function startFunc(callable $function, $args = [], $phpBin = null)
{
$funcBody = FunctionParser::genTmp($function);
$args['body'] = $funcBody;
$this -> start('callbackStub.php', $args);
$this -> start('callbackStub.php', $args, $phpBin);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Exception/PHPBinNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: shixi_qingzhe
* Date: 18/8/10
* Time: 下午3:41
*/

namespace Mutilprocessing\Exception;


class PHPBinNotFoundException extends \Exception
{
}

0 comments on commit 33f1916

Please sign in to comment.