Skip to content

Commit 08b038c

Browse files
authored
Merge pull request #61 from reactphp-parallel/3.x-run-examples-in-fiber
[3.x] Ensure examples run in a fiber
2 parents dd4fe07 + 34921b0 commit 08b038c

File tree

7 files changed

+107
-97
lines changed

7 files changed

+107
-97
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Cees-Jan Kiewiet
3+
Copyright (c) 2025 Cees-Jan Kiewiet
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+14-18
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,52 @@ ReactPHP bindings around ext-parallel-infinite-pool
1414
To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `~`.
1515

1616
```
17-
composer require react-parallel/infinite-pool
17+
composer require react-parallel/infinite-pool
1818
```
1919

2020
## Usage ##
2121

22-
The following example will spin up a thread with a 1 second TTL clean up policy. Meaning that threads are kept around
23-
for 1 second waiting for something to do before closed. It then runs a closure in the thread that will wait for one
24-
second before returning an message. Upon receiving that message the mean thread will echo out that message before
22+
The following example will spin up a thread with a 1 second TTL clean up policy. Meaning that threads are kept around
23+
for 1 second waiting for something to do before closed. It then runs a closure in the thread that will wait for one
24+
second before returning an message. Upon receiving that message the mean thread will echo out that message before
2525
closing the pool;
2626

2727
```php
28-
use React\EventLoop\Factory;
2928
use ReactParallel\EventLoop\EventLoopBridge;
3029
use ReactParallel\Pool\Infinite\Infinite;
3130

32-
$loop = Factory::create();
33-
$infinite = new Infinite($loop, new EventLoopBridge($loop), 1);
34-
$infinite->run(function () {
35-
sleep(1);
31+
$infinite = new Infinite(new EventLoopBridge(), 1);
3632

37-
return 'Hoi!';
38-
})->then(function (string $message) use ($infinite) {
39-
echo $message, PHP_EOL;
33+
Loop::futureTick(async(static function () use ($infinite) {
34+
echo $infinite->run(function () {
35+
sleep(1);
36+
37+
return 'Hoi!';
38+
}), PHP_EOL;
4039
$infinite->close();
4140
});
42-
$loop->run();
4341
```
4442

4543
## Metrics
4644

4745
This package supports metrics through [`wyrihaximus/metrics`](https://github.com/wyrihaximus/php-metrics):
4846

4947
```php
50-
use React\EventLoop\Factory;
5148
use ReactParallel\EventLoop\EventLoopBridge;
5249
use ReactParallel\EventLoop\Metrics as EventLoopMetrics;
5350
use ReactParallel\Pool\Infinite\Infinite;
5451
use ReactParallel\Pool\Infinite\Metrics;
5552
use WyriHaximus\Metrics\Configuration;
5653
use WyriHaximus\Metrics\InMemory\Registry;
5754

58-
$loop = Factory::create();
5955
$registry = new Registry(Configuration::create());
60-
$eventLoopBridge = (new EventLoopBridge($loop))->withMetrics(EventLoopMetrics::create($registry));
61-
$finite = (new Infinite($loop, $eventLoopBridge, 1.3))->withMetrics(Metrics::create($registry));
56+
$eventLoopBridge = (new EventLoopBridge())->withMetrics(EventLoopMetrics::create($registry));
57+
$infinite = (new Infinite($eventLoopBridge, 1.3))->withMetrics(Metrics::create($registry));
6258
```
6359

6460
## License ##
6561

66-
Copyright 2020 [Cees-Jan Kiewiet](http://wyrihaximus.net/)
62+
Copyright 2025 [Cees-Jan Kiewiet](http://wyrihaximus.net/)
6763

6864
Permission is hereby granted, free of charge, to any person
6965
obtaining a copy of this software and associated documentation

examples/echo.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
declare(strict_types=1);
44

5+
use React\EventLoop\Loop;
56
use ReactParallel\EventLoop\EventLoopBridge;
67
use ReactParallel\Pool\Infinite\Infinite;
8+
use function React\Async\async;
79

810
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
911

1012
$infinite = new Infinite(new EventLoopBridge(), 1);
11-
echo $infinite->run(static function (): string {
12-
sleep(1);
1313

14-
return 'Hoi!';
15-
}), PHP_EOL;
14+
Loop::futureTick(async(static function () use ($infinite) {
15+
echo $infinite->run(function () {
16+
sleep(1);
1617

17-
$infinite->close();
18+
return 'Hoi!';
19+
}), PHP_EOL;
20+
$infinite->close();
21+
}));

examples/exception.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
declare(strict_types=1);
44

5+
use React\EventLoop\Loop;
56
use ReactParallel\EventLoop\EventLoopBridge;
67
use ReactParallel\Pool\Infinite\Infinite;
78

89
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
910

1011
$infinite = new Infinite(new EventLoopBridge(), 1);
1112

12-
try {
13-
$infinite->run(static function (): void {
14-
throw new RuntimeException('Whoops I did it again!');
15-
});
16-
} finally {
17-
$infinite->close();
18-
}
13+
Loop::futureTick(async(static function () use ($infinite) {
14+
try {
15+
$infinite->run(static function (): void {
16+
throw new RuntimeException('Whoops I did it again!');
17+
});
18+
} finally {
19+
$infinite->close();
20+
}
21+
}));

examples/json.php

+31-28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\EventLoop\Loop;
66
use ReactParallel\EventLoop\EventLoopBridge;
77

8+
use ReactParallel\Pool\Infinite\Infinite;
89
use function React\Async\async;
910
use function React\Async\await;
1011
use function React\Promise\all;
@@ -15,34 +16,36 @@
1516

1617
$infinite = new Infinite(new EventLoopBridge(), 1);
1718

18-
$promises = [];
19-
$signalHandler = static function () use ($infinite): void {
20-
Loop::stop();
21-
$infinite->close();
22-
};
23-
24-
$tick = async(static function () use (&$promises, $infinite, $signalHandler, $json, &$tick): void {
25-
if (count($promises) < 1000) {
26-
$promises[] = async(static fn (string $json): string => $infinite->run(static function ($json): string {
27-
$json = json_decode($json, true);
28-
29-
return md5(json_encode($json));
30-
}, [$json]))($json);
31-
Loop::futureTick($tick);
32-
33-
return;
34-
}
35-
36-
try {
37-
var_export(await(all($promises)));
38-
} finally {
39-
$infinite->close();
40-
Loop::removeSignal(SIGINT, $signalHandler);
19+
Loop::futureTick(async(static function () use ($infinite, $json) {
20+
$promises = [];
21+
$signalHandler = static function () use ($infinite): void {
4122
Loop::stop();
42-
}
43-
});
44-
45-
Loop::futureTick($tick);
46-
Loop::addSignal(SIGINT, $signalHandler);
23+
$infinite->close();
24+
};
25+
26+
$tick = async(static function () use (&$promises, $infinite, $signalHandler, $json, &$tick): void {
27+
if (count($promises) < 1000) {
28+
$promises[] = async(static fn(string $json): string => $infinite->run(static function ($json): string {
29+
$json = json_decode($json, true);
30+
31+
return md5(json_encode($json));
32+
}, [$json]))($json);
33+
Loop::futureTick($tick);
34+
35+
return;
36+
}
37+
38+
try {
39+
var_export(await(all($promises)));
40+
} finally {
41+
$infinite->close();
42+
Loop::removeSignal(SIGINT, $signalHandler);
43+
Loop::stop();
44+
}
45+
});
46+
47+
Loop::futureTick($tick);
48+
Loop::addSignal(SIGINT, $signalHandler);
49+
}));
4750

4851
echo 'Loop::run()', PHP_EOL;

examples/sleep.php

+25-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use React\EventLoop\Loop;
66
use ReactParallel\EventLoop\EventLoopBridge;
77
use ReactParallel\Pool\Infinite\Infinite;
8-
use ReactParallel\Pool\Limited\Limited;
98

109
use function React\Async\async;
1110
use function React\Async\await;
@@ -16,34 +15,36 @@
1615

1716
$infinite = new Infinite(new EventLoopBridge(), 0.1);
1817

19-
$timer = Loop::addPeriodicTimer(1, static function () use ($infinite): void {
20-
var_export(iteratorOrArrayToArray($infinite->info()));
21-
});
18+
Loop::futureTick(async(static function () use ($infinite) {
19+
$timer = Loop::addPeriodicTimer(1, static function () use ($infinite): void {
20+
var_export(iteratorOrArrayToArray($infinite->info()));
21+
});
2222

23-
$promises = [];
24-
foreach (range(0, 250) as $i) {
25-
$promises[] = async(static function (Infinite $infinite, int $i): int {
26-
$sleep = $infinite->run(static function (int $sleep): int {
27-
sleep($sleep);
23+
$promises = [];
24+
foreach (range(0, 250) as $i) {
25+
$promises[] = async(static function (Infinite $infinite, int $i): int {
26+
$sleep = $infinite->run(static function (int $sleep): int {
27+
sleep($sleep);
2828

29-
return $sleep;
30-
}, [random_int(1, 13)]);
29+
return $sleep;
30+
}, [random_int(1, 13)]);
3131

32-
echo $i, '; ', $sleep, PHP_EOL;
32+
echo $i, '; ', $sleep, PHP_EOL;
3333

34-
return $sleep;
35-
})($infinite, $i);
36-
}
34+
return $sleep;
35+
})($infinite, $i);
36+
}
3737

38-
$signalHandler = static function () use ($infinite): void {
39-
$infinite->close();
40-
Loop::stop();
41-
};
38+
$signalHandler = static function () use ($infinite): void {
39+
$infinite->close();
40+
Loop::stop();
41+
};
4242

43-
Loop::addSignal(SIGINT, $signalHandler);
43+
Loop::addSignal(SIGINT, $signalHandler);
4444

45-
await(all($promises));
45+
await(all($promises));
4646

47-
$infinite->close();
48-
Loop::removeSignal(SIGINT, $signalHandler);
49-
Loop::cancelTimer($timer);
47+
$infinite->close();
48+
Loop::removeSignal(SIGINT, $signalHandler);
49+
Loop::cancelTimer($timer);
50+
}));

examples/versions.php

+17-14
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@
66
use React\EventLoop\Loop;
77
use ReactParallel\EventLoop\EventLoopBridge;
88
use ReactParallel\Pool\Infinite\Infinite;
9+
use function React\Async\async;
910

1011
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
1112

1213
$infinite = new Infinite(new EventLoopBridge(), 0.1);
1314

14-
Loop::addTimer(1, static function () use ($infinite): void {
15-
$infinite->kill();
16-
Loop::stop();
17-
});
15+
Loop::futureTick(async(static function () use ($infinite) {
16+
Loop::addTimer(1, static function () use ($infinite): void {
17+
$infinite->kill();
18+
Loop::stop();
19+
});
1820

19-
var_export(
20-
$infinite->run(
21-
static fn (): array => array_merge(
22-
...array_map(
23-
static fn (string $package): array => [
24-
$package => InstalledVersions::getPrettyVersion($package),
25-
],
26-
InstalledVersions::getInstalledPackages(),
21+
var_export(
22+
$infinite->run(
23+
static fn (): array => array_merge(
24+
...array_map(
25+
static fn (string $package): array => [
26+
$package => InstalledVersions::getPrettyVersion($package),
27+
],
28+
InstalledVersions::getInstalledPackages(),
29+
)
2730
)
2831
)
29-
)
30-
);
32+
);
33+
}));

0 commit comments

Comments
 (0)