Axync: Run several tasks using cooperative multitasking

Recommend this page to a friend!
  Info   Documentation   View files (3)   Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
69%Total: 399 All time: 6,465 This week: 308
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: 26
All time rank: 129911 in Egypt Egypt
Week rank: 243 8 in 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  
File Role Description
axync.php Class Class source
LICENSE Lic. License
README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:399
This week:0
All time:6,465
This week:308
 User Ratings  
 
 All time
Utility:100%
Consistency:100%
Documentation:90%
Examples:-
Tests:-
Videos:-
Overall:69%
Rank:504
  

For more information send a message to info at phpclasses dot org.