Skip to content

Commit 2f32b4d

Browse files
committed
[2.x] Ensure all examples are working as expected are run everything in a fiber
1 parent 0641e9c commit 2f32b4d

File tree

5 files changed

+73
-49
lines changed

5 files changed

+73
-49
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,28 @@ ReactPHP bindings around ext-parallel
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/limited-pool
17+
composer require react-parallel/limited-pool
1818
```
1919

2020
## Usage
2121

22-
Just like any other `react-parallel` the limited pool will run any closure you send to it. With the exception that this
22+
Just like any other `react-parallel` the limited pool will run any closure you send to it. With the exception that this
2323
pool have a fixed number of threads running.
2424

2525
```php
26-
$loop = Factory::create();
27-
2826
$finite = new Limited(
29-
new Infinite($loop, new EventLoopBridge($loop), 1), // Another pool, preferably an inifinite pool
27+
new Infinite(new EventLoopBridge(), 1), // Another pool, preferably an inifinite pool
3028
100 // The amount of threads to start and keep running
3129
);
3230
$time = time();
33-
$finite->run(function (int $time): int {
31+
echo 'Unix timestamp: ', $finite->run(function (int $time): int {
3432
return $time;
35-
}, [$time])->then(function (int $time): void {
36-
echo 'Unix timestamp: ', $time, PHP_EOL;
37-
})->done();
33+
}, [$time]), $time, PHP_EOL;
3834
```
3935

4036
## License ##
4137

42-
Copyright 2020 [Cees-Jan Kiewiet](http://wyrihaximus.net/)
38+
Copyright 2025 [Cees-Jan Kiewiet](http://wyrihaximus.net/)
4339

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

examples/echo.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use React\EventLoop\Loop;
6+
use ReactParallel\EventLoop\EventLoopBridge;
7+
use ReactParallel\Pool\Infinite\Infinite;
8+
use ReactParallel\Pool\Limited\Limited;
9+
use function React\Async\async;
10+
11+
require __DIR__ . '/../vendor/autoload.php';
12+
13+
$limited = new Limited(
14+
new Infinite(new EventLoopBridge(), 1), // Another pool, preferably an inifinite pool
15+
100 // The amount of threads to start and keep running
16+
);
17+
$time = time();
18+
19+
Loop::futureTick(async(static function () use ($limited, $time) {
20+
echo 'Unix timestamp: ', $limited->run(function (int $time): int {
21+
return $time;
22+
}, [$time]), $time, PHP_EOL;
23+
}));

examples/sleep.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,38 @@
1414

1515
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
1616

17-
$finite = new Limited(new Infinite(new EventLoopBridge(), 1), 100);
17+
$limited = new Limited(new Infinite(new EventLoopBridge(), 1), 100);
1818

19-
$timer = Loop::addPeriodicTimer(1, function () use ($finite) {
20-
var_export(iteratorOrArrayToArray($finite->info()));
21-
});
19+
Loop::futureTick(async(static function () use ($limited) {
20+
$timer = Loop::addPeriodicTimer(1, function () use ($limited) {
21+
var_export(iteratorOrArrayToArray($limited->info()));
22+
});
2223

23-
$promises = [];
24-
foreach (range(0, 250) as $i) {
25-
$promises[] = async(static function (Limited $finite, int $i): int {
26-
$sleep = $finite->run(static function (int $sleep): int {
27-
sleep($sleep);
24+
$promises = [];
25+
foreach (range(0, 250) as $i) {
26+
$promises[] = async(static function (Limited $limited, int $i): int {
27+
$sleep = $limited->run(static function (int $sleep): int {
28+
sleep($sleep);
2829

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

32-
echo $i, '; ', $sleep, PHP_EOL;
33+
echo $i, '; ', $sleep, PHP_EOL;
3334

34-
return $sleep;
35-
})($finite, $i);
36-
}
35+
return $sleep;
36+
})($limited, $i);
37+
}
3738

38-
$signalHandler = static function () use ($finite): void {
39-
$finite->close();
40-
Loop::stop();
41-
};
39+
$signalHandler = static function () use ($limited): void {
40+
$limited->close();
41+
Loop::stop();
42+
};
4243

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

45-
await(all($promises));
46+
await(all($promises));
4647

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

examples/versions.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@
77
use ReactParallel\EventLoop\EventLoopBridge;
88
use ReactParallel\Pool\Infinite\Infinite;
99
use ReactParallel\Pool\Limited\Limited;
10+
use function React\Async\async;
1011

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

1314
$limited = new Limited(new Infinite(new EventLoopBridge(), 1), 2);
1415

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

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

0 commit comments

Comments
 (0)