Last Updated | | Ratings | | Unique User Downloads | | Download Rankings |
2016-02-18 (2 years ago) | | 69% | | Total: 381 | | All time: 6,526 This week: 487 |
|
Description | | Author |
This class can run several tasks using cooperative multitasking.
It can takes one or more callback functions that will do the work of different tasks.
The class will execute the registered functions in a loop until all functions are finished.
The functions should use the PHP yield command after they do their work, so they allow the next function to do its part of the work.
The class can also return a generator that can be used by another object of the class to iterate over a list of callable functions or generators, thus implemented nested cooperative multitasking. Innovation Award
March 2016
Number 2 |
Cooperative multitasking is a way to execute multiple tasks concurrently. Only one task runs at each moment but it gives the turn to the next task in the list of concurrent tasks.
This class implements a manager for cooperative multitasking in pure PHP that relies on generators to let one task decide when it can give the control to the next task.
Manuel Lemos |
| |
|
|
Innovation award
Nominee: 2x
Winner: 1x |
|
Details
axync
a smart cooperative multitasking kernel for php7 (only) .
show me the code !
Example 1 (simple)
require "axync.php";
// our workers builder
$build = function($seq){
// this will be the generated coroutine
// yes, it MUST yield !
return (function() use($seq) {
for ( $i = 0; $i < 10; $i ++ ) {
printf("Worker (%d): i'm in the step no. %d \n", $seq, $i);
yield;
}
});
};
// create new manager
(new Axync(
$build(1),
$build(2),
$build(3)
))->exec();
// save your file as ~/tst1.php
// open your terminal and `php ~/tst1.php` and see the result .
Example 2 (advanced)
require "axync.php";
// our workers builder
$build = function($seq){
// this will be the generated coroutine
// yes, it MUST yield !
return (function() use($seq) {
for ( $i = 0; $i < 10; $i ++ ) {
printf("Worker (%d): i'm in the step no. %d \n", $seq, $i);
yield;
}
});
};
// create new manager
// and convert to to a new Generator !!
$manager1 = (new Axync(
$build(1),
$build(2),
$build(3)
))->toGenerator();
// create a new manager that handles new generators
(new Axync(
$build(10),
$build(20),
$build(30),
$manager1 // yes, axync support nested axync of axync too ;)
))->exec();
// save your file as ~/tst2.php
// open your terminal and `php ~/tst2.php` and see the result .
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.