forked from amal/AzaThread
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.php
255 lines (212 loc) · 5.88 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
use Aza\Components\CliBase\Base;
use Aza\Components\LibEvent\EventBase;
use Aza\Components\Thread\Exceptions\Exception;
use Aza\Components\Thread\SimpleThread;
use Aza\Components\Thread\Thread;
use Aza\Components\Thread\ThreadPool;
require __DIR__ . '/../vendor/autoload.php';
//require __DIR__ . '/../../../../example.bootstrap.php';
/**
* AzaThread examples
*
* @project Anizoptera CMF
* @package system.thread
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*/
/**
* Test thread
*/
class TestThreadReturnFirstArgument extends Thread
{
/**
* {@inheritdoc}
*/
function process()
{
return $this->getParam(0);
}
}
/**
* Test thread
*/
class TestThreadEvents extends Thread
{
const EV_PROCESS = 'process';
/**
* {@inheritdoc}
*/
function process()
{
$events = $this->getParam(0);
for ($i = 0; $i < $events; $i++) {
$this->trigger(self::EV_PROCESS, $i);
}
}
}
// Checks
if (!Thread::$useForks) {
echo PHP_EOL, "You do not have the minimum system requirements to work in async mode!!!";
if (!Base::$hasForkSupport) {
echo PHP_EOL, "You don't have pcntl or posix extensions installed or either not CLI SAPI environment!";
}
if (!EventBase::$hasLibevent) {
echo PHP_EOL, "You don't have libevent extension installed!";
}
echo PHP_EOL;
}
// ----------------------------------------------
echo PHP_EOL,
'Simple example with one thread',
PHP_EOL;
$num = 10; // Number of tasks
$thread = new TestThreadReturnFirstArgument();
// You can override preforkWait property
// to TRUE to not wait thread at first time manually
$thread->wait();
for ($i = 0; $i < $num; $i++) {
$value = $i;
// Run task and wait for the result
if ($thread->run($value)->wait()->getSuccess()) {
// Success
$result = $thread->getResult();
echo 'result: ' . $result . PHP_EOL;
} else {
// Error handling here
// processing is not successful if thread dies
// when worked or working timeout exceeded
echo 'error' . PHP_EOL;
}
}
// After work it's strongly recommended to clean
// resources obviously to avoid leaks
$thread->cleanup();
// ----------------------------------------------
echo PHP_EOL,
'Simple example with thread events',
PHP_EOL;
$events = 10; // Number of events
$num = 3; // Number of tasks
$thread = new TestThreadEvents();
// You can override preforkWait property
// to TRUE to not wait thread at first time manually
$thread->wait();
$cb = function($event_name, $event_data) {
echo "event: $event_name : ", $event_data, PHP_EOL;
};
$thread->bind(TestThreadEvents::EV_PROCESS, $cb);
for ($i = 0; $i < $num; $i++) {
$thread->run($events)->wait();
echo 'task ended', PHP_EOL;
}
// After work it's strongly recommended to clean
// resources obviously to avoid leaks
$thread->cleanup();
// ----------------------------------------------
$threads = 4;
echo PHP_EOL,
"Simple example with pool of threads ($threads)",
PHP_EOL;
$pool = new ThreadPool('TestThreadReturnFirstArgument', $threads);
$num = 25; // Number of tasks
$left = $num; // Number of remaining tasks
do {
while ($left > 0 && $pool->hasWaiting()) {
if (!$threadId = $pool->run($left)) {
throw new Exception('Pool slots error');
}
$left--;
}
if ($results = $pool->wait($failed)) {
foreach ($results as $threadId => $result) {
$num--;
echo "result: $result (thread $threadId)", PHP_EOL;
}
}
if ($failed) {
// Error handling here
// processing is not successful if thread dies
// when worked or working timeout exceeded
foreach ($failed as $threadId => $err) {
list($errorCode, $errorMessage) = $err;
echo "error (thread $threadId): #$errorCode - $errorMessage", PHP_EOL;
$left++;
}
}
} while ($num > 0);
// After work it's strongly recommended to clean
// resources obviously to avoid leaks
$pool->cleanup();
// ----------------------------------------------
$threads = 8;
$jobs = range(1, 30);
$jobs_num = count($jobs);
echo PHP_EOL,
"Example with pool of threads ($threads) and pool of jobs ($jobs_num)",
PHP_EOL;
$pool = new ThreadPool('TestThreadReturnFirstArgument', $threads);
$num = $jobs_num; // Number of tasks
$left = $jobs_num; // Number of remaining tasks
$started = array();
do {
while ($left > 0 && $pool->hasWaiting()) {
$task = array_shift($jobs);
if (!$threadId = $pool->run($task)) {
throw new Exception('Pool slots error');
}
$started[$threadId] = $task;
$left--;
}
if ($results = $pool->wait($failed)) {
foreach ($results as $threadId => $result) {
unset($started[$threadId]);
$num--;
echo "result: $result (thread $threadId)", PHP_EOL;
}
}
if ($failed) {
// Error handling here
// processing is not successful if thread dies
// when worked or working timeout exceeded
foreach ($failed as $threadId => $err) {
list($errorCode, $errorMessage) = $err;
$jobs[] = $started[$threadId];
echo "error: {$started[$threadId]} ",
"(thread $threadId): #$errorCode - $errorMessage", PHP_EOL;
unset($started[$threadId]);
$left++;
}
}
} while ($num > 0);
// After work it's strongly recommended to clean
// resources obviously to avoid leaks
$pool->cleanup();
// ----------------------------------------------
echo PHP_EOL,
'Simple example with one "closure" thread',
PHP_EOL;
$num = 10; // Number of tasks
$thread = SimpleThread::create(function($arg) {
return $arg;
});
// "closure" threads are not preforked by default
// and not multitask too. You can change it via
// the second argument of `SimpleThread::create`.
for ($i = 0; $i < $num; $i++) {
$value = $i;
// Run task and wait for the result
if ($thread->run($value)->wait()->getSuccess()) {
// Success
$result = $thread->getResult();
echo 'result: ' . $result . PHP_EOL;
} else {
// Error handling here
// processing is not successful if thread dies
// when worked or working timeout exceeded
echo 'error' . PHP_EOL;
}
}
// After work it's strongly recommended to clean
// resources obviously to avoid leaks
$thread->cleanup();