-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ProcessQueue feature (#7)
- Loading branch information
Showing
7 changed files
with
155 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Changelog | ||
|
||
## Release [v5.0.0](https://github.com/sweikenb/pcntl/releases/tag/v5.0.0) | ||
|
||
**Features** | ||
|
||
- `ProcessQueue` added as more flexible alternative to `ProcessPool` but without pre-created pool workers | ||
|
||
**Breaking Changes** | ||
|
||
- The return value of the optional `ProcessManager::wait`-callback is now used to determine if the method should | ||
continue to wait for further children to exit. If a value other than explicitly `false` is returned, it will continue | ||
to wait. | ||
|
||
* * * | ||
|
||
## Release [v4.0.0](https://github.com/sweikenb/pcntl/releases/tag/v4.0.0) | ||
|
||
**Plan for future releases** | ||
|
||
- Introduction of a maintained changelog for each release | ||
- From now on, only major version releases will introduce BC breaks | ||
- Features that are about to be removed in the next major version will be marked with `@deprecated` | ||
|
||
**Bugfixes** | ||
|
||
- [#4 Bug: very fast/empty forks will exit before the pcntl_wait() can capture it](https://github.com/sweikenb/pcntl/issues/4) | ||
|
||
**Breaking Changes** | ||
|
||
- PHP v8.2 as minimum requirement | ||
- Due to incompatibility, this library DOES NOT work if the **grpc** PHP-extension is installed | ||
- Removal of the optional `EventDispatcher` | ||
- Some methods of the `ProcessPool` and `ProcessManager` has been renamed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
use Sweikenb\Library\Pcntl\Api\ChildProcessInterface; | ||
use Sweikenb\Library\Pcntl\Api\ParentProcessInterface; | ||
use Sweikenb\Library\Pcntl\ProcessQueue; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$maxNumberOfThreadsToRunParallel = 4; | ||
|
||
$queue = new ProcessQueue($maxNumberOfThreadsToRunParallel); | ||
|
||
for ($i = 1; $i <= 50; $i++) { | ||
$queue->addToQueue(function (ChildProcessInterface $child, ParentProcessInterface $parent) use ($i) { | ||
sleep(mt_rand(1, 3)); | ||
fwrite(STDOUT, sprintf("Queued thread %d processed message no. %d\n", $child->getId(), $i)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Sweikenb\Library\Pcntl\Api; | ||
|
||
interface ProcessQueueInterface | ||
{ | ||
/** | ||
* Adds the given callback to the queue for execution. | ||
*/ | ||
public function addToQueue(callable $callback): ChildProcessInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Sweikenb\Library\Pcntl; | ||
|
||
use Sweikenb\Library\Pcntl\Api\ChildProcessInterface; | ||
use Sweikenb\Library\Pcntl\Api\ProcessManagerInterface; | ||
use Sweikenb\Library\Pcntl\Api\ProcessQueueInterface; | ||
|
||
class ProcessQueue implements ProcessQueueInterface | ||
{ | ||
private ProcessManagerInterface $processManager; | ||
private int $maxThreads; | ||
private int $threadCounter = 0; | ||
|
||
public function __construct( | ||
int $maxThreads, | ||
?ProcessManagerInterface $processManager = null | ||
) { | ||
$this->processManager = $processManager ?? new ProcessManager(); | ||
$this->maxThreads = max(1, $maxThreads); | ||
} | ||
|
||
public function addToQueue(callable $callback): ChildProcessInterface | ||
{ | ||
while ($this->threadCounter >= $this->maxThreads) { | ||
$this->processManager->wait(fn() => --$this->threadCounter >= $this->maxThreads); | ||
} | ||
$this->threadCounter++; | ||
return $this->processManager->runProcess($callback); | ||
} | ||
} |