-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-manager-wait-all.php
More file actions
65 lines (50 loc) · 1.89 KB
/
03-manager-wait-all.php
File metadata and controls
65 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
/**
* This file is part of fast-forward/fork.
*
* This source file is subject to the license bundled
* with this source code in the file LICENSE.
*
* @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/fork
* @see https://github.com/php-fast-forward
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
use FastForward\Fork\Examples\Support\ExampleConsole;
use FastForward\Fork\Manager\ForkManager;
use FastForward\Fork\Worker\WorkerInterface;
require __DIR__ . '/bootstrap.php';
$console = new ExampleConsole();
$console->title(
'03 Manager wait all',
'Create more than one group, wait for one group explicitly, then use $manager->wait() with no arguments.',
);
$manager = new ForkManager();
$shortGroup = $manager->fork(
static function (WorkerInterface $worker): int {
echo sprintf("short worker %d started\n", $worker->getPid());
usleep(120_000);
echo sprintf("short worker %d finished\n", $worker->getPid());
return 0;
},
2,
);
$longGroup = $manager->fork(
static function (WorkerInterface $worker): int {
echo sprintf("long worker %d started\n", $worker->getPid());
usleep(350_000);
echo sprintf("long worker %d finished\n", $worker->getPid());
return 0;
},
2,
);
$console->line('Waiting only for the short group with $shortGroup->wait().');
$shortGroup->wait();
$console->printGroup('Short group after $shortGroup->wait()', $shortGroup);
$console->printWorkers('Long group workers still running', $longGroup->getRunning());
$console->line('Waiting for every remaining worker created by the manager with $manager->wait().');
$manager->wait();
$console->printGroup('Long group after $manager->wait()', $longGroup);