Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hhxsv5/php-coroutine
A lightweight library to implement coroutine by yield & Generator.
https://github.com/hhxsv5/php-coroutine
coroutine generator yield
Last synced: 3 months ago
JSON representation
A lightweight library to implement coroutine by yield & Generator.
- Host: GitHub
- URL: https://github.com/hhxsv5/php-coroutine
- Owner: hhxsv5
- License: mit
- Created: 2017-08-11T09:05:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-08-17T08:54:56.000Z (over 7 years ago)
- Last Synced: 2024-03-29T16:22:57.724Z (10 months ago)
- Topics: coroutine, generator, yield
- Language: PHP
- Size: 11.7 KB
- Stars: 27
- Watchers: 3
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
PHP Coroutine
======A lightweight library to implement coroutine by yield & Generator.
## Requirements
* PHP 5.5 or later
## Installation via Composer([packagist](https://packagist.org/packages/hhxsv5/php-coroutine))
```BASH
composer require "hhxsv5/php-coroutine:~1.0" -vvv
```## Usage
### Run demo- PHP 5.5+
```PHP
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+
```PHP
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](https://github.com/hhxsv5/php-coroutine/blob/master/LICENSE)