Skip to content

Commit

Permalink
+add return value support for callable func
Browse files Browse the repository at this point in the history
+add get func return and echo data function
=fix a bug when executing callable func
=update README
  • Loading branch information
郭庆哲 committed Jul 6, 2018
1 parent 5cba595 commit e2e4019
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 42 deletions.
59 changes: 57 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- By using this tool, PHP scripts can be invoked asynchronously based on multi processes, and finally wait for each process to return results, saving lots of time.

- graceful and efficient

- can get callable function return value

## Installation
You can use composer to install this library from the command line.
Expand All @@ -39,6 +41,8 @@ Async::create()->run('task.php', ['runTest'.$i]);

### distribute tasks by a simple function and async execute

!!! WARN:please don't insert a '&&&' string in echo and return for some reason it will break down the program run

```php
<?php

Expand All @@ -62,14 +66,61 @@ Async::create()->startFunc($func, ['param1' => 'hello', 'param2' => ' PHP']);
<?php

use Mutilprocessing\Async;

$outData = [];
Async::join(function($code, $out, $err) use(&$outData) {
// var_dump($code, $out, $err);
$outData = $out;
// you can handle code runtime exception like this
if (strlen($err) != 0) {
// do sth.
}
// and you can get return value like this
// more function detail see examples :)
array_push($outData, \Mutilprocessing\Async::getReturn($out));
});

```

outData structure:

echos represent echos in execute

returns represent return in execute

```
array(4) {
[0] =>
array(2) {
'echos' =>
string(5) "hello"
'returns' =>
string(0) ""
}
[1] =>
array(2) {
'echos' =>
string(6) "KZ RNG"
'returns' =>
string(10) "return 777"
}
[2] =>
array(2) {
'echos' =>
string(17) "EDG AFSreturn 888"
'returns' =>
string(0) ""
}
[3] =>
array(2) {
'echos' =>
string(6) "SKT RW"
'returns' =>
string(10) "return 666"
}
}
```


### getArgs passed from Async::start

```php
Expand Down Expand Up @@ -101,6 +152,10 @@ Async::discard();
* public static function discard()
* public static function wait(callable $logHandler = null)
* public static function getArgs($argv = null)
* public static function getReturn($out)
* ### FunctionParser
* ### option shorthand
* public static function genTmp(callable $function)



43 changes: 26 additions & 17 deletions examples/task_dispath.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,41 @@
require_once __DIR__ .'/bootstrap.php';

$func = function ($test1, $test2)
{
return "return 777";
};

$func2 = function ($test1, $test2)
{
echo $test1.$test2;
echo PHP_EOL."666";
echo PHP_EOL."777";
echo PHP_EOL."888";
echo PHP_EOL."999";
if ($test2) {
echo "this is if";
}

for ($i = 1;$i < 10;$i++) {
echo $i;
if (false) {
echo "6666";
} else {
echo "as65d4";
}
echo PHP_EOL."999";
echo PHP_EOL."999";
echo PHP_EOL."999";

return "return 666";


};

\Mutilprocessing\Async::create()->startFunc(function () {
echo "hello";
}, ['test1' => 'KZ', 'test2' => ' RNG']);
$func1 = function ($test1, $test2)
{
echo $test1.$test2;

\Mutilprocessing\Async::create()->startFunc($func, ['test1' => 'KZ', 'test2' => ' RNG']);
echo "return 888";
};

\Mutilprocessing\Async::create()->startFunc($func2, ['test1' => 'SKT', 'test2' => ' RW']);
\Mutilprocessing\Async::create()->startFunc($func, ['test1' => 'SKT', 'test2' => ' RW']);

$outData = [];
\Mutilprocessing\Async::wait(function ($code, $out, $err) use (&$outData) {
var_dump($out);
if (strlen($err) != 0) {
print_r($err);
}
array_push($outData, \Mutilprocessing\Async::getReturn($out));
});

var_dump($outData);
17 changes: 0 additions & 17 deletions examples/task_func.php

This file was deleted.

9 changes: 9 additions & 0 deletions src/Async.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public static function getArgs($argv = null)
return json_decode(base64_decode($argv), 1);
}

public static function getReturn($return)
{
$returns = explode("&&&", $return);
return [
'echos' => $returns[0],
'returns' => isset($returns[1]) ? base64_decode($returns[1]) : null
];
}


}

12 changes: 8 additions & 4 deletions src/FunctionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ public static function genTmp(callable $function)
$headPtr = 0;
$footPtr = 0;
$codeBlockStr = implode('', $codeBlock);
$i = 0;$j = strlen($codeBlockStr) - 1;
$i = 0;
$j = strlen($codeBlockStr) - 1;
while($i <= $j) {
if ($headFound == true && $footFound = true) {
if ($headFound == true && $footFound == true) {
break;
}
if ($codeBlockStr[$i] == "{") {
$headPtr = $i;
$headFound = true;
} else {
$i++;

}
$i++;
if ($codeBlockStr[$j] == "}") {
$footPtr = $j;
$footFound = true;
} else {
$j--;
}
$j--;
}
$codeBlockStrReal = substr($codeBlockStr, $headPtr + 1, $footPtr - $headPtr - 1);
return $codeBlockStrReal;
Expand Down
7 changes: 5 additions & 2 deletions src/callbackStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
* Time: 下午4:41
*/


$fn = function($code, $args){
extract($args, EXTR_SKIP);
return eval($code);
};
$args_decode = json_decode(base64_decode($argv[1]), 1);

$fn($args_decode['body'], $args_decode);
$return = $fn($args_decode['body'], $args_decode);

$returnData = base64_encode($return);

echo "&&&".$returnData;

0 comments on commit e2e4019

Please sign in to comment.