-
Notifications
You must be signed in to change notification settings - Fork 10
Work
Stefano Azzolini edited this page Sep 1, 2014
·
4 revisions
The Work module allow you to execute multiple jobs in parallel, via cooperative multitasking.
Notice:
This feature needs Generators, so PHP version must be at least 5.5 to be used, a fatal error is triggered if version requirement is not met.
Workers must be generators, so the yield
keywords must be present. When a worker is registered, an object of class TaskCoroutine
is returned.
Work::add(function(){
for ($i = 0; $i <= 10; $i++) {
echo "Value: $i \n";
yield; // Pass control to other workers
}
});
You can also assign a name to the worker :
Work::add('the_counter',function(){
for ($i = 0; $i <= 10; $i++) {
echo "Value: $i \n";
yield; // Pass control to other workers
}
});
All registered workers will be executed and the method will release control after all of the workers have finished their job.
Work::run();
Work::add(function(){
for ($i = 0; $i <= 20; $i++) {
echo "A: $i \n";
yield;
}
});
Work::add(function(){
for ($i = 0; $i <= 10; $i++) {
echo "B: $i \n";
yield;
}
});
// Run the workers
Work::run();
Output:
A:0
B:0
A:1
B:1
A:2
B:2
A:3
B:3
A:4
B:4
A:5
B:5
A:6
B:6
A:7
B:7
A:8
B:8
A:9
B:9
A:10
B:10
A:11
A:12
A:13
A:14
A:15
A:16
A:17
A:18
A:19
A:20
Core is maintained by using the Semantic Versioning Specification (SemVer).
Copyright 2014-2016 Caffeina srl under the MIT license.
http://caffeina.com