-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.php
54 lines (36 loc) · 1.05 KB
/
demo.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
<?php
require 'vendor/autoload.php';
$manager = new \Dmkit\Fork\Manager\Manager;
// you can pass any callable to the addWorker function
// for more info about callable - http://php.net/manual/en/language.types.callable.php
// pass an anonymous function
$manager->addWorker( function(\Dmkit\Fork\Worker\Message\Message $msg) {
echo "Child Process 1 - Started\n";
for($i=1; $i <=5; $i++) {
echo "$i\n";
}
echo "Child Process 1 - Ended\n\n";
$msg->set('Child Process 1 - Success');
});
// pass an object
class MyTask
{
public function run(\Dmkit\Fork\Worker\Message\Message $msg) {
echo "Child Process 2 - Started\n";
for($i=1; $i <=5; $i++) {
echo "$i\n";
}
echo "Child Process 2 - Ended\n\n";
$msg->set(['success'=>1, 'workerName'=> 'MyTask']);
}
}
$mytaks = new MyTask;
$manager->addWorker([$mytaks, 'run']);
// run the workers
$manager->dispatch();
// parse the messages passed in the workers for error checking or whatever you want
$messages = $manager->getMessages();
foreach($messages as $msg) {
echo "\n";
print_r($msg->get());
}