Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Cees-Jan Kiewiet
Copyright (c) 2025 Cees-Jan Kiewiet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,28 @@ ReactPHP bindings around ext-parallel
To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `~`.

```
composer require react-parallel/limited-pool
composer require react-parallel/limited-pool
```

## Usage

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

```php
$loop = Factory::create();

$finite = new Limited(
new Infinite($loop, new EventLoopBridge($loop), 1), // Another pool, preferably an inifinite pool
new Infinite(new EventLoopBridge(), 1), // Another pool, preferably an inifinite pool
100 // The amount of threads to start and keep running
);
$time = time();
$finite->run(function (int $time): int {
echo 'Unix timestamp: ', $finite->run(function (int $time): int {
return $time;
}, [$time])->then(function (int $time): void {
echo 'Unix timestamp: ', $time, PHP_EOL;
})->done();
}, [$time]), $time, PHP_EOL;
```

## License ##

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

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
23 changes: 23 additions & 0 deletions examples/echo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use React\EventLoop\Loop;
use ReactParallel\EventLoop\EventLoopBridge;
use ReactParallel\Pool\Infinite\Infinite;
use ReactParallel\Pool\Limited\Limited;
use function React\Async\async;

require __DIR__ . '/../vendor/autoload.php';

$limited = new Limited(
new Infinite(new EventLoopBridge(), 1), // Another pool, preferably an inifinite pool
100 // The amount of threads to start and keep running
);
$time = time();

Loop::futureTick(async(static function () use ($limited, $time) {
echo 'Unix timestamp: ', $limited->run(function (int $time): int {
return $time;
}, [$time]), $time, PHP_EOL;
}));
50 changes: 26 additions & 24 deletions examples/sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,38 @@

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

$finite = new Limited(new Infinite(new EventLoopBridge(), 1), 100);
$limited = new Limited(new Infinite(new EventLoopBridge(), 1), 100);

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

$promises = [];
foreach (range(0, 250) as $i) {
$promises[] = async(static function (Limited $finite, int $i): int {
$sleep = $finite->run(static function (int $sleep): int {
sleep($sleep);
$promises = [];
foreach (range(0, 250) as $i) {
$promises[] = async(static function (Limited $limited, int $i): int {
$sleep = $limited->run(static function (int $sleep): int {
sleep($sleep);

return $sleep;
}, [random_int(1, 13)]);
return $sleep;
}, [random_int(1, 13)]);

echo $i, '; ', $sleep, PHP_EOL;
echo $i, '; ', $sleep, PHP_EOL;

return $sleep;
})($finite, $i);
}
return $sleep;
})($limited, $i);
}

$signalHandler = static function () use ($finite): void {
$finite->close();
Loop::stop();
};
$signalHandler = static function () use ($limited): void {
$limited->close();
Loop::stop();
};

Loop::addSignal(SIGINT, $signalHandler);
Loop::addSignal(SIGINT, $signalHandler);

await(all($promises));
await(all($promises));

$finite->close();
Loop::removeSignal(SIGINT, $signalHandler);
Loop::cancelTimer($timer);
$limited->close();
Loop::removeSignal(SIGINT, $signalHandler);
Loop::cancelTimer($timer);
}));
31 changes: 17 additions & 14 deletions examples/versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
use ReactParallel\EventLoop\EventLoopBridge;
use ReactParallel\Pool\Infinite\Infinite;
use ReactParallel\Pool\Limited\Limited;
use function React\Async\async;

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

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

Loop::addTimer(1, static function () use ($limited): void {
$limited->kill();
Loop::stop();
});
Loop::futureTick(async(static function () use ($limited) {
Loop::addTimer(1, static function () use ($limited): void {
$limited->kill();
Loop::stop();
});

var_export(
$limited->run(
static fn (): array => array_merge(
...array_map(
static fn (string $package): array => [
$package => InstalledVersions::getPrettyVersion($package),
],
InstalledVersions::getInstalledPackages(),
var_export(
$limited->run(
static fn (): array => array_merge(
...array_map(
static fn (string $package): array => [
$package => InstalledVersions::getPrettyVersion($package),
],
InstalledVersions::getInstalledPackages(),
)
)
)
)
);
);
}));
Loading