|
1 | | -# worker-pool |
| 1 | +# Worker Pool |
| 2 | + |
| 3 | +[](https://travis-ci.com/reactphp-parallel/worker-pool) |
| 4 | +[](https://packagist.org/packages/react-parallel/worker-pool) |
| 5 | +[](https://packagist.org/packages/react-parallel/worker-pool) |
| 6 | +[](https://packagist.org/packages/react-parallel/worker-pool) |
| 7 | + |
2 | 8 | Create pool with only one specific designated task |
| 9 | + |
| 10 | +## Install ## |
| 11 | + |
| 12 | +To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `~`. |
| 13 | + |
| 14 | +``` |
| 15 | +composer require react-parallel/worker-pool |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage ## |
| 19 | + |
| 20 | +```php |
| 21 | +<?php |
| 22 | + |
| 23 | +use Money\Money; |
| 24 | +use React\EventLoop\Factory; |
| 25 | +use ReactParallel\Factory as ParallelFactory; |
| 26 | +use ReactParallel\Pool\Worker\Workers\ReturnWorkerFactory; |
| 27 | +use ReactParallel\Pool\Worker\Workers\Work; |
| 28 | + |
| 29 | +require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php'; |
| 30 | + |
| 31 | +$loop = Factory::create(); |
| 32 | +$parallelFactory = new ParallelFactory($loop); |
| 33 | +$workerFactory = new ReturnWorkerFactory(); |
| 34 | + |
| 35 | +$pool = new \ReactParallel\Pool\Worker\Worker($parallelFactory, $workerFactory, 133); |
| 36 | +$pool->perform(new Work(Money::EUR(512)))->always(function () use ($pool) { |
| 37 | + $pool->close(); |
| 38 | +})->done(function (Money $money) { |
| 39 | + echo $money->getAmount(); |
| 40 | +}); |
| 41 | + |
| 42 | +$loop->run(); |
| 43 | +``` |
| 44 | + |
| 45 | +## Creating your own worker ## |
| 46 | + |
| 47 | +A worker consists of two classes, a factory and a worker created by the factory. Due to the nature `ext-parallel` works |
| 48 | +it is expensive to transfer classes and scalars to threads. Workers, are designed to reuse that same thread for as long |
| 49 | +as there is work. To get an idea of how it works lets have a look at the return worker. First we create the factory, |
| 50 | +which is a data transfer object to kickstart from the main thread to the worker thread. Anything you put into it will |
| 51 | +be sent to the worker thread. The less you put in there the better. Once the factory is send to the thread the |
| 52 | +`construct` method on it will be called to create the actual worker. |
| 53 | + |
| 54 | +```php |
| 55 | +<?php |
| 56 | + |
| 57 | +declare(strict_types=1); |
| 58 | + |
| 59 | +namespace ReactParallel\Pool\Worker\Workers; |
| 60 | + |
| 61 | +use ReactParallel\Pool\Worker\Work\Worker as WorkerInterface; |
| 62 | +use ReactParallel\Pool\Worker\Work\WorkerFactory; |
| 63 | + |
| 64 | +final class ReturnWorkerFactory implements WorkerFactory |
| 65 | +{ |
| 66 | + public function construct(): WorkerInterface |
| 67 | + { |
| 68 | + return new ReturnWorker(); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +After it created the worker it will listen for incoming work and call the `perform` method for each work DTO coming in. |
| 74 | +The `ReturnWorker` will simply sent the work it received back to the main thread. (This is a very simple and effective |
| 75 | +way to tests that threads are working as intended, and it is included for anyone wanting to use this package as an easy |
| 76 | +set of tools to get started.) |
| 77 | + |
| 78 | +```php |
| 79 | +<?php |
| 80 | + |
| 81 | +declare(strict_types=1); |
| 82 | + |
| 83 | +namespace ReactParallel\Pool\Worker\Workers; |
| 84 | + |
| 85 | +use ReactParallel\Pool\Worker\Work as WorkContract; |
| 86 | +use ReactParallel\Pool\Worker\Work\Worker as WorkerInterface; |
| 87 | + |
| 88 | +final class ReturnWorker implements WorkerInterface |
| 89 | +{ |
| 90 | + public function perform(WorkContract $work): Result |
| 91 | + { |
| 92 | + return new Result($work->work()); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Now how the factory creates the worker and what it puts into it is all up to you. You already have the Composer |
| 98 | +autoloader active so you can create any object like you normally would. |
| 99 | + |
| 100 | +## License ## |
| 101 | + |
| 102 | +Copyright 2020 [Cees-Jan Kiewiet](http://wyrihaximus.net/) |
| 103 | + |
| 104 | +Permission is hereby granted, free of charge, to any person |
| 105 | +obtaining a copy of this software and associated documentation |
| 106 | +files (the "Software"), to deal in the Software without |
| 107 | +restriction, including without limitation the rights to use, |
| 108 | +copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 109 | +copies of the Software, and to permit persons to whom the |
| 110 | +Software is furnished to do so, subject to the following |
| 111 | +conditions: |
| 112 | + |
| 113 | +The above copyright notice and this permission notice shall be |
| 114 | +included in all copies or substantial portions of the Software. |
| 115 | + |
| 116 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 117 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 118 | +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 119 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 120 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 121 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 122 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 123 | +OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments