Skip to content

Commit 462812f

Browse files
committed
Update
1 parent 9276e5c commit 462812f

4 files changed

Lines changed: 33 additions & 17 deletions

File tree

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ All you need to have installed:
1313
[![License](https://poser.pugx.org/wapmorgan/threadable/license)](https://packagist.org/packages/wapmorgan/threadable)
1414

1515
1. Structure
16-
- What is a `Threadable`?
1716
- What is a `Worker`?
1817
* How to create your Worker
1918
- What is a `WorkersPool`?
@@ -22,8 +21,8 @@ All you need to have installed:
2221
- One worker
2322
- Few workers with `WorkersPool`
2423
4. API
25-
- `Worker` secrets and important methods
26-
- `WorkersPool` features
24+
- `Worker` API
25+
- `WorkersPool` API
2726
5. Predefined workers
2827
- `DownloadWorker`
2928
6. Use cases
@@ -61,7 +60,7 @@ class SleepingWorker extends Worker
6160
It takes care of all maintenance, payload dispatching and life-cycle of workers. Allows you change the size of the pool dynamically and other useful stuff.
6261

6362
# Simple usage
64-
For example, you want to background downloading work. Let's use `BackgroundWork` class to background it and show progress for user (or store in DB/...).
63+
For example, you want to just background downloading work. Let's use `wapmorgan\Threadable\BackgroundWork` class to background it and show progress for user (or store in DB/...).
6564

6665
Everything you need to do:
6766
1. Prepare payloads for `DownloadWorker`
@@ -72,6 +71,10 @@ Everything you need to do:
7271
`DownloadWorker` needs an array with `source` and `target` elements. Prepare it:
7372

7473
```php
74+
use wapmorgan\Threadable\BackgroundWork;
75+
use wapmorgan\Threadable\DownloadWorker;
76+
use wapmorgan\Threadable\Worker;
77+
7578
$file_sources = ['https://yandex.ru/images/today?size=1920x1080', 'http://hosting-obzo-ru.1gb.ru/hosting-obzor.ru.zip'];
7679
$files = [];
7780
foreach ($file_sources as $file_to_download) {
@@ -85,6 +88,7 @@ foreach ($file_sources as $file_to_download) {
8588

8689
## Stage 2. Launching in background
8790

91+
### One-thread worker
8892
Run it in one thread with `doInBackground` function. Signature is following:
8993

9094
`doInBackground(Worker $worker,
@@ -97,10 +101,10 @@ Run it in one thread with `doInBackground` function. Signature is following:
97101
- `$payloads` - an array of all payloads.
98102
- `$payloadHandlingCallback` - a callback that will be called every `$sleepMicrotime` microseconds with information about currently running payload.
99103
Signature for callback: `(Worker $worker, int $payloadI, $payloadData)`
100-
- `$onPayloadFinishCallback` - a callback that will be called when worker ends with one payload.
104+
- `$onPayloadFinishCallback` - a callback that will be called when worker ends with one payload.
101105
Signature for callback: `(Worker $worker, int $payloadI, $payloadData, $payloadResult)`
102106

103-
So, collect all information run it:
107+
So, collect all information to run it:
104108

105109
```php
106110
$result = BackgroundWork::doInBackground(new DownloadWorker(), $files,
@@ -119,7 +123,9 @@ if ($result)
119123

120124
Example is in `bin/example_file_downloading_easy` file.
121125

122-
To run in in few threads use `doInBackgroundParallel` function with signature:
126+
### Few-threads worker
127+
128+
To run it in few threads use `doInBackgroundParallel`. It has almost the same signature as one-thread function:
123129

124130
`doInBackgroundParallel(Worker $worker,
125131
array $payloads,
@@ -128,6 +134,8 @@ To run in in few threads use `doInBackgroundParallel` function with signature:
128134
$sleepMicroTime = 1000,
129135
$poolSize = self::BY_CPU_NUMBER)`
130136

137+
By adjusting `$poolSize` you can select number of workers that should be used.
138+
131139
Example is in `bin/example_file_downloading_pool_easy` file.
132140

133141
# How it works

bin/example_file_downloading_easy

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wapmorgan\Threadable\Worker;
77

88
require_once __DIR__.'/../vendor/autoload.php';
99

10-
$file_sources = ['https://yandex.ru/images/today?size=1920x1080', 'http://hosting-obzo-ru.1gb.ru/hosting-obzor.ru.zip'];
10+
$file_sources = ['https://yandex.ru/images/today?size=1920x1080', 'http://hosting-obzo-ru.1gb.ru/hosting-obzor.ru.zip', 'http://soft.eurodir.ru/test-speed-100Mb.bin'];
1111
$files = [];
1212
foreach ($file_sources as $file_to_download) {
1313
$files[] = [
@@ -17,15 +17,16 @@ foreach ($file_sources as $file_to_download) {
1717
];
1818
}
1919

20-
BackgroundWork::doInBackground(new DownloadWorker(), $files, function (Worker $worker, $payloadI, $payloadData) {
20+
$result = BackgroundWork::doInBackground(new DownloadWorker(), $files, function (Worker $worker, $payloadI, $payloadData) {
2121
clearstatcache(true, $payloadData['target']);
2222
echo "\r" . '#' . ($payloadI + 1) . '. ' . basename($payloadData['source']) . ' downloading ' . round(filesize($payloadData['target']) * 100 / $payloadData['size'], 2) . '%';
2323
}, function (Worker $worker, $payloadI, $payloadData, $payloadResult) {
24-
echo "\r" . '#' . ($payloadI + 1) . '. ' . basename($payloadData['source']) . ' successfully downloaded' . PHP_EOL;
24+
echo "\r" . '#' . ($payloadI + 1) . '. ' . basename($payloadData['source']) . ' successfully downloaded (size '.filesize($payloadData['target']).')' . PHP_EOL;
2525
return true;
2626
});
2727

28-
foreach ($files as $file)
29-
unlink($file['target']);
30-
28+
if ($result)
29+
echo 'All files downloaded successfully'.PHP_EOL;
3130

31+
foreach ($files as $file)
32+
if (file_exists($file['target'])) unlink($file['target']);

bin/example_file_downloading_pool_easy

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wapmorgan\Threadable\Worker;
77
use wapmorgan\Threadable\WorkersPool;
88
require_once __DIR__.'/../vendor/autoload.php';
99

10-
$file_sources = [/*'https://yandex.ru/images/today?size=1920x1080', */'http://hosting-obzo-ru.1gb.ru/hosting-obzor.ru.zip', 'http://soft.eurodir.ru/test-speed-100Mb.bin'];
10+
$file_sources = ['https://yandex.ru/images/today?size=1920x1080', 'http://hosting-obzo-ru.1gb.ru/hosting-obzor.ru.zip', 'http://soft.eurodir.ru/test-speed-100Mb.bin'];
1111
$files = [];
1212
foreach ($file_sources as $file_to_download) {
1313
$file_size = DownloadWorker::getRemoteFileSize($file_to_download);
@@ -19,10 +19,16 @@ foreach ($file_sources as $file_to_download) {
1919
];
2020
}
2121

22-
BackgroundWork::doInBackgroundParallel(new DownloadWorker(), $files, function (Worker $worker, $payloadI, $payloadData) {
22+
$result = BackgroundWork::doInBackgroundParallel(new DownloadWorker(), $files, function (Worker $worker, $payloadI, $payloadData) {
2323
clearstatcache(true, $payloadData['target']);
2424
echo "\r" . '#' . ($payloadI + 1) . '. ' . basename($payloadData['source']) . ' downloading ' . round(filesize($payloadData['target']) * 100 / $payloadData['size'], 2) . '%';
2525
}, function (Worker $worker, $payloadI, $payloadData, $payloadResult) {
26-
echo "\r" . '#' . ($payloadI + 1) . '. ' . basename($payloadData['source']) . ' successfully downloaded' . PHP_EOL;
26+
echo "\r" . '#' . ($payloadI + 1) . '. ' . basename($payloadData['source']) . ' successfully downloaded (size '.filesize($payloadData['target']).')' . PHP_EOL;
2727
return true;
2828
});
29+
30+
if ($result)
31+
echo 'All files downloaded successfully'.PHP_EOL;
32+
33+
foreach ($files as $file)
34+
if (file_exists($file['target'])) unlink($file['target']);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
],
1313
"require": {
1414
"ext-posix": "*",
15-
"ext-pcntl": "*"
15+
"ext-pcntl": "*",
16+
"ext-sockets": "*"
1617
},
1718
"require-dev": {
1819
"phpunit/phpunit": "~4.8"

0 commit comments

Comments
 (0)