Skip to content

Commit f2baf3e

Browse files
authored
Merge pull request #9 from reactphp-parallel/add-documentation
Add Documentation
2 parents f8f5902 + 021a35e commit f2baf3e

File tree

11 files changed

+715
-331
lines changed

11 files changed

+715
-331
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Cees-Jan Kiewiet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,123 @@
1-
# worker-pool
1+
# Worker Pool
2+
3+
[![Build Status](https://travis-ci.com/reactphp-parallel/worker-pool.png)](https://travis-ci.com/reactphp-parallel/worker-pool)
4+
[![Latest Stable Version](https://poser.pugx.org/react-parallel/worker-pool/v/stable.png)](https://packagist.org/packages/react-parallel/worker-pool)
5+
[![Total Downloads](https://poser.pugx.org/react-parallel/worker-pool/downloads.png)](https://packagist.org/packages/react-parallel/worker-pool)
6+
[![License](https://poser.pugx.org/react-parallel/worker-pool/license.png)](https://packagist.org/packages/react-parallel/worker-pool)
7+
28
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.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"ext-parallel": "*",
1515
"react-parallel/contracts": "^1.0",
1616
"react-parallel/event-loop": "^1.0",
17+
"react-parallel/react-parallel": "dev-master",
1718
"react/event-loop": "^1.1",
1819
"react/promise": "^2.7",
1920
"wyrihaximus/constants": "^1.5",

0 commit comments

Comments
 (0)