-
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.
* IPC implementation
- Loading branch information
Showing
27 changed files
with
759 additions
and
43 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
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
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,75 @@ | ||
<?php | ||
|
||
use Sweikenb\Library\Pcntl\Api\ChildProcessInterface; | ||
use Sweikenb\Library\Pcntl\Api\ParentProcessInterface; | ||
use Sweikenb\Library\Pcntl\Factory\MessageFactory; | ||
use Sweikenb\Library\Pcntl\ProcessManager; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$numWorker = 4; | ||
$numMessages = 25; | ||
|
||
$pm = new ProcessManager(); | ||
$factory = new MessageFactory(); | ||
|
||
$workers = []; | ||
/* @var array<int, ChildProcessInterface> $workers */ | ||
|
||
for ($i = 0; $i < $numWorker; $i++) { | ||
$workers[$i] = $pm->runProcess( | ||
function (ChildProcessInterface $process, ParentProcessInterface $parentProcess) use ($i, $factory) { | ||
fwrite( | ||
STDOUT, | ||
sprintf("> Worker #%d: started and ready to process messages\n", ($i + 1)) | ||
); | ||
$count = 0; | ||
while ($message = $parentProcess->getNextMessage()) { | ||
$count++; | ||
fwrite( | ||
STDOUT, | ||
sprintf( | ||
">> Worker #%d: received a message: '%s' '%s' (no. msg.: %d)\n", | ||
($i + 1), | ||
$message->getTopic(), | ||
$message->getPayload(), | ||
$count | ||
) | ||
); | ||
$parentProcess->sendMessage( | ||
$factory->create( | ||
sprintf('Answer from #%d', $process->getId()), | ||
sprintf("msg %d", $count) | ||
) | ||
); | ||
} | ||
} | ||
); | ||
} | ||
|
||
for ($i = 0; $i < $numWorker * $numMessages; $i++) { | ||
$workerId = $i % $numWorker; | ||
$message = $factory->create('some message for ' . ($workerId + 1), 'some payload for ' . ($workerId + 1)); | ||
$workers[$workerId]->sendMessage($message); | ||
} | ||
|
||
foreach ($workers as $i => $worker) { | ||
$count = 0; | ||
while ($count < $numMessages) { | ||
$count++; | ||
$message = $worker->getNextMessage(); | ||
fwrite( | ||
STDOUT, | ||
sprintf( | ||
">> Worker #%d answered with message: '%s' '%s'\n", | ||
$worker->getId(), | ||
$message->getTopic(), | ||
$message->getPayload() | ||
) | ||
); | ||
} | ||
|
||
// stop the worker process | ||
fwrite(STDOUT, sprintf('# Stopping worker (%d)', $worker->getId())); | ||
$worker->kill(); | ||
} |
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,26 @@ | ||
<?php | ||
|
||
use Sweikenb\Library\Pcntl\Factory\WorkerMessageFactory; | ||
use Sweikenb\Library\Pcntl\ProcessPool; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
require __DIR__ . '/ExampleWorkerService.php'; | ||
|
||
$numWorker = 4; | ||
$numMessages = 25 * $numWorker; | ||
$factory = new WorkerMessageFactory(); | ||
|
||
$pool = new ProcessPool($numWorker); | ||
for ($i = 0; $i < $numMessages; $i++) { | ||
$pool->sendMessage( | ||
$factory->create('hello_world', ExampleWorkerService::class) | ||
); | ||
} | ||
|
||
// Give the workers some time to work. | ||
// Usually you would send messages in some kid of event/endless-loop and/or with some custom unblock logic. | ||
sleep(3); | ||
|
||
// Work done, kill all workers! | ||
// HINT: if you skipp this kill, the main process and its worker will run infinitely | ||
$pool->killAll(); |
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; | ||
|
||
class ExampleWorkerService | ||
{ | ||
public function __invoke(ChildProcessInterface $childProcess, ParentProcessInterface $parentProcess): void | ||
{ | ||
fwrite( | ||
STDOUT, | ||
sprintf( | ||
"Hello world message handled by worker #%s\n", | ||
$childProcess->getId() | ||
) | ||
); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Sweikenb\Library\Pcntl\Api\Ipc; | ||
|
||
use Serializable; | ||
|
||
interface MessageInterface extends Serializable | ||
{ | ||
/** | ||
* Must return a topic that might be used for later message routing. | ||
* | ||
* @return string | ||
*/ | ||
public function getTopic(): string; | ||
|
||
/** | ||
* Payload of the message that can be anything that is "serializable()". | ||
* | ||
* @return mixed | ||
*/ | ||
public function getPayload(): mixed; | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace Sweikenb\Library\Pcntl\Api\Ipc; | ||
|
||
use Sweikenb\Library\Pcntl\Api\ChildProcessInterface; | ||
use Sweikenb\Library\Pcntl\Api\ParentProcessInterface; | ||
use Sweikenb\Library\Pcntl\Api\ProcessPoolInterface; | ||
|
||
interface WorkerMessageInterface extends MessageInterface | ||
{ | ||
public function execute(ProcessPoolInterface $processPool, ChildProcessInterface $childProcess, ParentProcessInterface $parentProcess): void; | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace Sweikenb\Library\Pcntl\Api\Ipc; | ||
|
||
interface WorkerSelectionStrategyInterface | ||
{ | ||
public function configure(array $processIds): void; | ||
|
||
public function getNextWorkerPid(): int; | ||
} |
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
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,14 @@ | ||
<?php | ||
|
||
namespace Sweikenb\Library\Pcntl\Api; | ||
|
||
use Sweikenb\Library\Pcntl\Api\Ipc\WorkerMessageInterface; | ||
|
||
interface ProcessPoolInterface | ||
{ | ||
public function getInvocationBuilder(): callable; | ||
|
||
public function sendMessage(WorkerMessageInterface $workerMessage): bool; | ||
|
||
public function killAll(): void; | ||
} |
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
Oops, something went wrong.