PHP Classes
elePHPant
Icontem

Axync: Run several tasks using cooperative multitasking

Recommend this page to a friend!
  Info   View files Documentation   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-02-18 (2 years ago) RSS 2.0 feedStarStarStarStar 69%Total: 381 All time: 6,526 This week: 487Up
Version License PHP version Categories
axync 1.0MIT/X Consortium ...7.0Language, PHP 7
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
PHP Programming Innovation award nominee
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
  Performance   Level  
Name: Mohammed Al Ashaal <contact>
Classes: 8 packages by
Country: Egypt Egypt
Innovation award
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 .
  Files folder image Files  
File Role Description
Plain text file axync.php Class Class source
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:381
This week:0
All time:6,526
This week:487Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:90%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:488