2017 © Pedro Peláez
 

library php-coroutine

A lightweight library to implement coroutine by yield & Generator.

image

hhxsv5/php-coroutine

A lightweight library to implement coroutine by yield & Generator.

  • Thursday, August 17, 2017
  • by hhxsv5
  • Repository
  • 1 Watchers
  • 12 Stars
  • 2,042 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 5 Versions
  • 2 % Grown

The README.md

PHP Coroutine

A lightweight library to implement coroutine by yield & Generator., (*1)

Requirements

  • PHP 5.5 or later

Installation via Composer(packagist)

composer require "hhxsv5/php-coroutine:~1.0" -vvv

Usage

Run demo

  • PHP 5.5+
include '../vendor/autoload.php';

use Hhxsv5\Coroutine\Scheduler;

$start = microtime(true);
/**
 * @param mixed & $return
 * @return Generator
 */
function task1(&$return)
{
    echo 'task1:start ', microtime(true), PHP_EOL;
    $return = yield file_get_contents('http://www.weather.com.cn/data/cityinfo/101270101.html');
    echo 'task1:end ', microtime(true), PHP_EOL;
}

/**
 * @param mixed & $return
 * @return Generator
 */
function task2(&$return)
{
    echo 'task2:start ', microtime(true), PHP_EOL;
    $return = yield file_get_contents('https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=yourtoken');
    echo 'task2:end ', microtime(true), PHP_EOL;
}

$scheduler = new Scheduler();

$t1 = task1($return1);
$t2 = task2($return2);


$scheduler->createTask($t1);
$scheduler->createTask($t2);

$scheduler->run();

var_dump($return1, $return2);

$end = microtime(true) - $start;
echo $end;
  • PHP7+
include '../vendor/autoload.php';

use Hhxsv5\Coroutine\Scheduler;

$start = microtime(true);
/**
 * @return Generator
 */
function task1()
{
    echo 'task1:start ', microtime(true), PHP_EOL;
    $ret = yield file_get_contents('http://www.weather.com.cn/data/cityinfo/101270101.html');
    echo 'task1:end ', microtime(true), PHP_EOL;
    return $ret;
}

/**
 * @return Generator
 */
function task2()
{
    echo 'task2:start ', microtime(true), PHP_EOL;
    $ret = yield file_get_contents('https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=yourtoken');
    echo 'task2:end ', microtime(true), PHP_EOL;
    return $ret;
}

$scheduler = new Scheduler();

$t1 = task1();
$t2 = task2();

$scheduler->createTask($t1);
$scheduler->createTask($t2);

$scheduler->run();

var_dump($t1->getReturn(), $t2->getReturn());//PHP 7+

$end = microtime(true) - $start;
echo $end;

License

MIT, (*2)

The Versions

17/08 2017

dev-master

9999999-dev https://github.com/hhxsv5/php-coroutine

A lightweight library to implement coroutine by yield & Generator.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Xie Biao

generator coroutine yield php-coroutine

15/08 2017

1.3.0

1.3.0.0 https://github.com/hhxsv5/php-coroutine

A lightweight library to implement coroutine by yield & Generator.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Xie Biao

generator coroutine yield php-coroutine

15/08 2017

1.2.0

1.2.0.0 https://github.com/hhxsv5/php-coroutine

A lightweight library to implement coroutine by yield & Generator.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Xie Biao

generator coroutine yield php-coroutine

14/08 2017

1.1.0

1.1.0.0 https://github.com/hhxsv5/php-coroutine

A lightweight library to implement coroutine by yield & Generator.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Xie Biao

generator coroutine yield php-coroutine

11/08 2017

1.0.0

1.0.0.0 https://github.com/hhxsv5/php-coroutine

A lightweight library to implement coroutine by yield & Generator.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Xie Biao

generator coroutine yield php-coroutine