PHP Classes

Axync: Run several tasks using cooperative multitasking

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 408 All time: 6,504 This week: 660Up
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
Picture of Mohammed Al Ashaal
  Performance   Level  
Name: Mohammed Al Ashaal <contact>
Classes: 8 packages by
Country: Egypt Egypt
Age: 31
All time rank: 127911 in Egypt Egypt
Week rank: 215 Up3 in Egypt Egypt Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Documentation

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 (3)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:408
This week:0
All time:6,504
This week:660Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:90%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:359