62 lines
No EOL
1.7 KiB
PHP
62 lines
No EOL
1.7 KiB
PHP
<?php
|
|
|
|
use LogLib\Log;
|
|
use TamerLib\Enums\TamerMode;
|
|
use TamerLib\Objects\ServerConfiguration;
|
|
use TamerLib\tm;
|
|
|
|
require 'ncc';
|
|
import('net.nosial.tamerlib');
|
|
|
|
// Start as client mode, if no configuration is passed on then
|
|
// Tamer will spawn its own Redis server and use it.
|
|
tm::initialize(TamerMode::CLIENT);
|
|
tm::createWorker(20, __DIR__ . DIRECTORY_SEPARATOR . 'worker.php');
|
|
|
|
$total_sleep = 0;
|
|
$start_time = time();
|
|
$jobs = [];
|
|
|
|
// Start doing programming!
|
|
// Loop 2 times, each time we will do a sleep job
|
|
for($i = 0; $i <= 2; $i++)
|
|
{
|
|
$sleep_time = random_int(5, 10);
|
|
$total_sleep += $sleep_time;
|
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
$job_id = tm::sleep($sleep_time);
|
|
|
|
// Log the result
|
|
print(sprintf('Created task %s', $job_id) . PHP_EOL);
|
|
$jobs[$job_id] = null;
|
|
}
|
|
|
|
// Loop 200 times, each time we will do a Pi calculation job
|
|
for($i = 0; $i < 200; $i++)
|
|
{
|
|
$iterations = random_int(100000, 1000000);
|
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
$job_id = tm::calculate_pi($iterations);
|
|
|
|
// Log the result
|
|
print(sprintf('Created task %s', $job_id) . PHP_EOL);
|
|
$jobs[$job_id] = null;
|
|
}
|
|
|
|
print('Waiting for jobs to finish...' . PHP_EOL);
|
|
|
|
// Wait for all jobs to finish
|
|
tm::wait(static function ($job_id, $result) use (&$jobs) {
|
|
print(sprintf('Job %s finished with result %s', $job_id, $result) . PHP_EOL);
|
|
$jobs[$job_id] = $result;
|
|
});
|
|
|
|
// Finally do some fancy calling
|
|
|
|
|
|
var_dump($jobs);
|
|
|
|
print(sprintf('Total sleep time: %s', $total_sleep) . PHP_EOL);
|
|
print(sprintf('Total execution time: %s', time() - $start_time) . PHP_EOL); |