-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.php
More file actions
92 lines (72 loc) · 2.84 KB
/
example.php
File metadata and controls
92 lines (72 loc) · 2.84 KB
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
<?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);
?>