Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example for error related behaviors of ping-pong.
- Loading branch information
Mike Tian-Jian Jiang
committed
Apr 20, 2016
1 parent
ec600e9
commit 37306d2
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
require dirname(dirname(__DIR__)) . '/vendor/autoload.php'; | ||
|
||
use React\ChildProcess\Process; | ||
use React\EventLoop\Factory; | ||
use WyriHaximus\React\ChildProcess\Pool\Factory\Flexible; | ||
use WyriHaximus\React\ChildProcess\Pool\PoolInterface; | ||
|
||
const POOL_PROCESS_COUNT = 4; | ||
const I = 3; | ||
|
||
$loop = Factory::create(); | ||
Flexible::create( | ||
new Process('php '. dirname(dirname(__DIR__)) .'/examples/ping-pong-with-error/pong.php'), | ||
$loop, | ||
[ 'min_size' => 1, 'max_size' => POOL_PROCESS_COUNT ] | ||
)->then(function (PoolInterface $pool) use ($loop) { | ||
$pool->on('message', function ($message) { | ||
var_export($message); | ||
}); | ||
|
||
$pool->on('error', function ($e) { | ||
echo 'Error: ', var_export($e, true), PHP_EOL; | ||
}); | ||
|
||
for ($i = 0; $i < I; $i++) { | ||
echo "$i "; | ||
$j = $i; | ||
$pool->rpc(\WyriHaximus\React\ChildProcess\Messenger\Messages\Factory::rpc('ping', [ | ||
'i' => $i, | ||
]))->then(function ($data) use ($j) { | ||
echo "Answer for $j * $j * $j * $j: ", $data['result'], PHP_EOL; | ||
}, function ($error) { | ||
var_export($error); | ||
die(); | ||
}); | ||
} | ||
echo PHP_EOL; | ||
|
||
$timer = $loop->addPeriodicTimer(0.5, function () use ($pool) { | ||
$status = 'Pool status: '; | ||
foreach ($pool->info() as $key => $value) { | ||
$status .= " $key: $value"; | ||
} | ||
echo $status, PHP_EOL; | ||
}); | ||
|
||
$loop->addTimer(2, function () use ($pool, $timer, $loop) { | ||
for ($i = 0; $i < I; $i++) { | ||
echo "$i "; | ||
$j = $i; | ||
$pool->rpc(\WyriHaximus\React\ChildProcess\Messenger\Messages\Factory::rpc('ping', [ | ||
'i' => $i, | ||
]))->then(function ($data) use ($j) { | ||
echo "Answer for $j * $j * $j * $j: ", $data['result'], PHP_EOL; | ||
}, function ($error) { | ||
var_export($error); | ||
die(); | ||
}); | ||
} | ||
echo PHP_EOL; | ||
|
||
$pool->rpc(\WyriHaximus\React\ChildProcess\Messenger\Messages\Factory::rpc('ping', [ | ||
'i' => ++$i, | ||
]))->then(function () use ($pool, $timer, $loop) { | ||
echo 'Terminating pool', PHP_EOL; | ||
$pool->terminate(\WyriHaximus\React\ChildProcess\Messenger\Messages\Factory::message([ | ||
'woeufh209h838392', | ||
])); | ||
$timer->cancel(); | ||
echo 'Done!!!', PHP_EOL; | ||
}); | ||
}); | ||
}); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
require dirname(dirname(__DIR__)) . '/vendor/autoload.php'; | ||
|
||
use React\EventLoop\Factory; | ||
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; | ||
use WyriHaximus\React\ChildProcess\Messenger\Messenger; | ||
|
||
error_reporting(-1); | ||
ini_set('display_errors', true); | ||
ini_set('error_log', 'err-pong.txt'); | ||
|
||
$loop = Factory::create(); | ||
$recipient = \WyriHaximus\React\ChildProcess\Messenger\Factory::child($loop); | ||
$recipient->on('message', function (Payload $payload, Messenger $messenger) use ($loop, $recipient) { | ||
trigger_error('An error not interrupting unless caught.'); | ||
try { | ||
file_put_contents( | ||
'log-pong.txt', | ||
'Got msg: '. var_export($payload->getPayload(), true) . PHP_EOL, | ||
FILE_APPEND | ||
); | ||
$messenger->message(\WyriHaximus\React\ChildProcess\Messenger\Messages\Factory::message([ | ||
'No one seems receiving this.' | ||
])); | ||
$recipient->message(\WyriHaximus\React\ChildProcess\Messenger\Messages\Factory::message([ | ||
'No one seems receiving this, either.' | ||
])); | ||
$loop->stop(); | ||
throw new Exception('An exception to be caught.'); | ||
} catch (Exception $e) { | ||
file_put_contents('exc-pong.txt', $e->getMessage() . PHP_EOL, FILE_APPEND); | ||
$messenger->softTerminate(); | ||
} | ||
throw new Exception('An exception to become an event.'); | ||
}); | ||
$recipient->on('error', function (Exception $e) { | ||
file_put_contents('exc-pong.txt', $e->getMessage() . PHP_EOL, FILE_APPEND); | ||
}); | ||
$recipient->registerRpc('ping', function (Payload $payload, Messenger $messenger) use ($loop) { | ||
$stopAt = time() + mt_rand(1, 2); | ||
|
||
do { | ||
// Do nothing | ||
} while ($stopAt >= time()); | ||
|
||
// trigger_error('An error to cause infinite loop if not caught.'); | ||
|
||
return \React\Promise\resolve([ | ||
'result' => $payload['i'] * $payload['i'] * $payload['i'] * $payload['i'], | ||
]); | ||
}); | ||
|
||
$loop->run(); |