-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
261 lines (231 loc) · 9.53 KB
/
README
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
256
257
258
259
260
ABOUT
PowerSpawn is a PCNTL and POSIX wrapper for *nix systems running PHP
It is for managing and running forked process for parrallel computing with PHP
NOTE
PowerSpawn has been replaced by PowerProcess which can be found here: https://github.com/lordgnu/PowerProcess
PowerProcess contains all the same features as PowerSpawn, but is easier to use and has more robust support
of the PCNTL functions. Additionally, PowerProcess allows simple daemonization of scripts and supports better
socket-based logging
HOW TO USE
First, be sure to include PowerSpawn in your source: require_once('/path/to/PowerSpawn.class.php')
Then, you'll want to initialize the PowerSpawn Object and setup your config. See the complete example below
EXAMPLE
<?php
// Include PowerSpawn
require_once('PowerSpawn.class.php');
// Initialize the object
$ps = new PowerSpawn;
// Create a callback function (Not required)
function allChildrenComplete() {
echo "All the children processes have completed\n";
}
function killedChild() {
echo "A child was just killed\n";
}
// Setup PowerSpawn Config
$ps->setCallback('allChildrenComplete'); // Calls the allChildrenComplete() function when all processes have checked in and there is no more work to complete
$ps->setKillCallback('killedChild'); // Calls the killedChild function when a child is executed for exceeding time limit
$ps->maxChildren = 5; // Only allows 5 children to be running at any given time
$ps->timeLimit = 10; // Kills children processes that run longer than 10 seconds
// Build an array with some fake data to proccess
$data = array();
for ($i = 0; $i < 15; $i++) {
$data[] = $i;
}
// Start the parent loop
while ($ps->runParentCode()) {
// Only the parent process will run code inside this loop
// Determine if we still have data to process
if (count($data)) {
// We still have data to process
// Check to see if we have an open slot to spawn a child
if ($ps->spawnReady()) {
// We can spawn a new process
$ps->childData = array_shift($data);
// Echo some text to let the user know that this is the parent and we are spawning a new child
echo "[PARENT] Spawning a new process\n";
// Spawn the process
$ps->spawnChild();
} else {
// All slots are full, just tick for now
$ps->Tick();
}
} else {
// There is no more data to process
// Echo some text to let the user know that the parent process is out of data
echo "[PARENT] Out of data to hand out - Waiting for all children to check in\n";
// Call the shutdown() method - blocks until all chilren have checked in or been killed because of executing too long
$ps->shutdown();
// All child processes have terminated
echo "[PARENT] All child processes have completed\n";
}
}
// Start the children code section
if ($ps->runChildCode()) {
// Only child processes forked by the parent will run this code
// The parent process will not execute this code
// Get the data for this run
$myNumber = $ps->childData;
// Let the user know the thread is running
echo "[CHILD:{$myNumber}] Running with number {$myNumber} - My PID is {$ps->myPID()}\n";
// Now we'll sleep x seconds where x is $myNumber - but we'll loop to show that we are doing work
$x = 0;
while ($x < $myNumber) {
sleep(1);
$x++;
echo "[CHILD:{$myNumber}] Just completed iteration {$x} of {$myNumber}\n";
}
// Let the user know that all work has been completed
// All children that run longer than 10 seconds won't make it this far with the settings above
echo "[CHILD:{$myNumber}] I have completed my work\n";
}
// Send signal 0 to let parent process know we are done
exit(0);
?>
OUPUT FROM EXAMPLE:
dbauer@zent:~/src/PowerSpawn$ php ./example.php
PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/mcrypt.ini on line 1 in Unknown on line 0
[PARENT] Spawning a new process
[PARENT] Spawning a new process
[CHILD:0] Running with number 0 - My PID is 22174
[CHILD:0] I have completed my work
[PARENT] Spawning a new process
[CHILD:1] Running with number 1 - My PID is 22175
[PARENT] Spawning a new process
[CHILD:2] Running with number 2 - My PID is 22176
[PARENT] Spawning a new process
[CHILD:3] Running with number 3 - My PID is 22177
[CHILD:4] Running with number 4 - My PID is 22178
[PARENT] Spawning a new process
[CHILD:5] Running with number 5 - My PID is 22179
[CHILD:1] Just completed iteration 1 of 1
[CHILD:1] I have completed my work
[CHILD:2] Just completed iteration 1 of 2
[CHILD:3] Just completed iteration 1 of 3
[CHILD:4] Just completed iteration 1 of 4
[PARENT] Spawning a new process
[CHILD:5] Just completed iteration 1 of 5
[CHILD:6] Running with number 6 - My PID is 22180
[CHILD:2] Just completed iteration 2 of 2
[CHILD:2] I have completed my work
[CHILD:3] Just completed iteration 2 of 3
[CHILD:4] Just completed iteration 2 of 4
[CHILD:5] Just completed iteration 2 of 5
[PARENT] Spawning a new process
[CHILD:6] Just completed iteration 1 of 6
[CHILD:7] Running with number 7 - My PID is 22181
[CHILD:3] Just completed iteration 3 of 3
[CHILD:3] I have completed my work
[CHILD:4] Just completed iteration 3 of 4
[CHILD:5] Just completed iteration 3 of 5
[CHILD:6] Just completed iteration 2 of 6
[CHILD:7] Just completed iteration 1 of 7
[PARENT] Spawning a new process
[CHILD:8] Running with number 8 - My PID is 22182
[CHILD:4] Just completed iteration 4 of 4
[CHILD:4] I have completed my work
[CHILD:5] Just completed iteration 4 of 5
[CHILD:6] Just completed iteration 3 of 6
[CHILD:7] Just completed iteration 2 of 7
[CHILD:8] Just completed iteration 1 of 8
[PARENT] Spawning a new process
[CHILD:9] Running with number 9 - My PID is 22183
[CHILD:5] Just completed iteration 5 of 5
[CHILD:5] I have completed my work
[CHILD:6] Just completed iteration 4 of 6
[CHILD:7] Just completed iteration 3 of 7
[CHILD:8] Just completed iteration 2 of 8
[CHILD:9] Just completed iteration 1 of 9
[PARENT] Spawning a new process
[CHILD:10] Running with number 10 - My PID is 22184
[CHILD:6] Just completed iteration 5 of 6
[CHILD:7] Just completed iteration 4 of 7
[CHILD:8] Just completed iteration 3 of 8
[CHILD:9] Just completed iteration 2 of 9
[CHILD:10] Just completed iteration 1 of 10
[CHILD:6] Just completed iteration 6 of 6
[CHILD:6] I have completed my work
[CHILD:7] Just completed iteration 5 of 7
[CHILD:8] Just completed iteration 4 of 8
[CHILD:9] Just completed iteration 3 of 9
[PARENT] Spawning a new process
[CHILD:10] Just completed iteration 2 of 10
[CHILD:11] Running with number 11 - My PID is 22185
[CHILD:7] Just completed iteration 6 of 7
[CHILD:8] Just completed iteration 5 of 8
[CHILD:9] Just completed iteration 4 of 9
[CHILD:10] Just completed iteration 3 of 10
[CHILD:11] Just completed iteration 1 of 11
[CHILD:7] Just completed iteration 7 of 7
[CHILD:7] I have completed my work
[CHILD:8] Just completed iteration 6 of 8
[CHILD:9] Just completed iteration 5 of 9
[PARENT] Spawning a new process
[CHILD:10] Just completed iteration 4 of 10
[CHILD:11] Just completed iteration 2 of 11
[CHILD:12] Running with number 12 - My PID is 22186
[CHILD:8] Just completed iteration 7 of 8
[CHILD:9] Just completed iteration 6 of 9
[CHILD:10] Just completed iteration 5 of 10
[CHILD:11] Just completed iteration 3 of 11
[CHILD:12] Just completed iteration 1 of 12
[CHILD:8] Just completed iteration 8 of 8
[CHILD:8] I have completed my work
[CHILD:9] Just completed iteration 7 of 9
[CHILD:10] Just completed iteration 6 of 10
[CHILD:11] Just completed iteration 4 of 11
[CHILD:12] Just completed iteration 2 of 12
[PARENT] Spawning a new process
[CHILD:13] Running with number 13 - My PID is 22188
[CHILD:9] Just completed iteration 8 of 9
[CHILD:10] Just completed iteration 7 of 10
[CHILD:11] Just completed iteration 5 of 11
[CHILD:12] Just completed iteration 3 of 12
[CHILD:13] Just completed iteration 1 of 13
[CHILD:9] Just completed iteration 9 of 9
[CHILD:9] I have completed my work
[CHILD:11] Just completed iteration 6 of 11
[CHILD:10] Just completed iteration 8 of 10
[CHILD:12] Just completed iteration 4 of 12
[CHILD:13] Just completed iteration 2 of 13
[PARENT] Spawning a new process
[PARENT] Out of data to hand out - Waiting for all children to check in
[CHILD:14] Running with number 14 - My PID is 22189
[CHILD:10] Just completed iteration 9 of 10
[CHILD:11] Just completed iteration 7 of 11
[CHILD:12] Just completed iteration 5 of 12
[CHILD:13] Just completed iteration 3 of 13
[CHILD:14] Just completed iteration 1 of 14
[CHILD:10] Just completed iteration 10 of 10
[CHILD:11] Just completed iteration 8 of 11
[CHILD:10] I have completed my work
[CHILD:12] Just completed iteration 6 of 12
[CHILD:13] Just completed iteration 4 of 13
A child was just killed
[CHILD:14] Just completed iteration 2 of 14
[CHILD:11] Just completed iteration 9 of 11
[CHILD:12] Just completed iteration 7 of 12
[CHILD:13] Just completed iteration 5 of 13
[CHILD:14] Just completed iteration 3 of 14
[CHILD:11] Just completed iteration 10 of 11
[CHILD:12] Just completed iteration 8 of 12
[CHILD:13] Just completed iteration 6 of 13
[CHILD:14] Just completed iteration 4 of 14
A child was just killed
[CHILD:12] Just completed iteration 9 of 12
[CHILD:13] Just completed iteration 7 of 13
[CHILD:14] Just completed iteration 5 of 14
[CHILD:12] Just completed iteration 10 of 12
[CHILD:13] Just completed iteration 8 of 13
[CHILD:14] Just completed iteration 6 of 14
A child was just killed
[CHILD:13] Just completed iteration 9 of 13
[CHILD:14] Just completed iteration 7 of 14
[CHILD:13] Just completed iteration 10 of 13
[CHILD:14] Just completed iteration 8 of 14
A child was just killed
[CHILD:14] Just completed iteration 9 of 14
[CHILD:14] Just completed iteration 10 of 14
A child was just killed
[PARENT] All child processes have completed
All the children processes have completed