Skip to content

Commit e2e4019

Browse files
author
郭庆哲
committed
+add return value support for callable func
+add get func return and echo data function =fix a bug when executing callable func =update README
1 parent 5cba595 commit e2e4019

6 files changed

Lines changed: 105 additions & 42 deletions

File tree

README.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- 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.
1818

1919
- graceful and efficient
20+
21+
- can get callable function return value
2022

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

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

44+
!!! WARN:please don't insert a '&&&' string in echo and return for some reason it will break down the program run
45+
4246
```php
4347
<?php
4448

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

6468
use Mutilprocessing\Async;
65-
69+
$outData = [];
6670
Async::join(function($code, $out, $err) use(&$outData) {
6771
// var_dump($code, $out, $err);
68-
$outData = $out;
72+
// you can handle code runtime exception like this
73+
if (strlen($err) != 0) {
74+
// do sth.
75+
}
76+
// and you can get return value like this
77+
// more function detail see examples :)
78+
array_push($outData, \Mutilprocessing\Async::getReturn($out));
6979
});
7080

7181
```
7282

83+
outData structure:
84+
85+
echos represent echos in execute
86+
87+
returns represent return in execute
88+
89+
```
90+
array(4) {
91+
[0] =>
92+
array(2) {
93+
'echos' =>
94+
string(5) "hello"
95+
'returns' =>
96+
string(0) ""
97+
}
98+
[1] =>
99+
array(2) {
100+
'echos' =>
101+
string(6) "KZ RNG"
102+
'returns' =>
103+
string(10) "return 777"
104+
}
105+
[2] =>
106+
array(2) {
107+
'echos' =>
108+
string(17) "EDG AFSreturn 888"
109+
'returns' =>
110+
string(0) ""
111+
}
112+
[3] =>
113+
array(2) {
114+
'echos' =>
115+
string(6) "SKT RW"
116+
'returns' =>
117+
string(10) "return 666"
118+
}
119+
}
120+
121+
```
122+
123+
73124
### getArgs passed from Async::start
74125

75126
```php
@@ -101,6 +152,10 @@ Async::discard();
101152
* public static function discard()
102153
* public static function wait(callable $logHandler = null)
103154
* public static function getArgs($argv = null)
155+
* public static function getReturn($out)
156+
* ### FunctionParser
157+
* ### option shorthand
158+
* public static function genTmp(callable $function)
104159

105160

106161

examples/task_dispath.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,41 @@
99
require_once __DIR__ .'/bootstrap.php';
1010

1111
$func = function ($test1, $test2)
12+
{
13+
return "return 777";
14+
};
15+
16+
$func2 = function ($test1, $test2)
1217
{
1318
echo $test1.$test2;
14-
echo PHP_EOL."666";
15-
echo PHP_EOL."777";
16-
echo PHP_EOL."888";
17-
echo PHP_EOL."999";
18-
if ($test2) {
19-
echo "this is if";
20-
}
2119

22-
for ($i = 1;$i < 10;$i++) {
23-
echo $i;
20+
if (false) {
21+
echo "6666";
22+
} else {
23+
echo "as65d4";
2424
}
25-
echo PHP_EOL."999";
26-
echo PHP_EOL."999";
27-
echo PHP_EOL."999";
25+
26+
return "return 666";
27+
2828

2929
};
3030

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

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

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

41+
$outData = [];
3842
\Mutilprocessing\Async::wait(function ($code, $out, $err) use (&$outData) {
39-
var_dump($out);
43+
if (strlen($err) != 0) {
44+
print_r($err);
45+
}
46+
array_push($outData, \Mutilprocessing\Async::getReturn($out));
4047
});
48+
49+
var_dump($outData);

examples/task_func.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Async.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public static function getArgs($argv = null)
124124
return json_decode(base64_decode($argv), 1);
125125
}
126126

127+
public static function getReturn($return)
128+
{
129+
$returns = explode("&&&", $return);
130+
return [
131+
'echos' => $returns[0],
132+
'returns' => isset($returns[1]) ? base64_decode($returns[1]) : null
133+
];
134+
}
135+
127136

128137
}
129138

src/FunctionParser.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,25 @@ public static function genTmp(callable $function)
2727
$headPtr = 0;
2828
$footPtr = 0;
2929
$codeBlockStr = implode('', $codeBlock);
30-
$i = 0;$j = strlen($codeBlockStr) - 1;
30+
$i = 0;
31+
$j = strlen($codeBlockStr) - 1;
3132
while($i <= $j) {
32-
if ($headFound == true && $footFound = true) {
33+
if ($headFound == true && $footFound == true) {
3334
break;
3435
}
3536
if ($codeBlockStr[$i] == "{") {
3637
$headPtr = $i;
3738
$headFound = true;
39+
} else {
40+
$i++;
41+
3842
}
39-
$i++;
4043
if ($codeBlockStr[$j] == "}") {
4144
$footPtr = $j;
4245
$footFound = true;
46+
} else {
47+
$j--;
4348
}
44-
$j--;
4549
}
4650
$codeBlockStrReal = substr($codeBlockStr, $headPtr + 1, $footPtr - $headPtr - 1);
4751
return $codeBlockStrReal;

src/callbackStub.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
* Time: 下午4:41
88
*/
99

10-
1110
$fn = function($code, $args){
1211
extract($args, EXTR_SKIP);
1312
return eval($code);
1413
};
1514
$args_decode = json_decode(base64_decode($argv[1]), 1);
1615

17-
$fn($args_decode['body'], $args_decode);
16+
$return = $fn($args_decode['body'], $args_decode);
17+
18+
$returnData = base64_encode($return);
19+
20+
echo "&&&".$returnData;

0 commit comments

Comments
 (0)