All Projects → hhxsv5 → php-coroutine

hhxsv5 / php-coroutine

Licence: MIT license
A lightweight library to implement coroutine by yield & Generator.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-coroutine

nj
NJ is a simple script engine in golang with Lua-like syntax.
Stars: ✭ 19 (-29.63%)
Mutual labels:  yield, coroutine
universe-topology
A universal computer knowledge topology for all the programmers worldwide.
Stars: ✭ 47 (+74.07%)
Mutual labels:  coroutine
Libaco
A blazing fast and lightweight C asymmetric coroutine library 💎 ⛅🚀⛅🌞
Stars: ✭ 2,918 (+10707.41%)
Mutual labels:  coroutine
Zanphp
PHP开发面向C10K+的高并发SOA服务 和RPC服务首选框架
Stars: ✭ 1,451 (+5274.07%)
Mutual labels:  yield
async
Asynchronous programming for R -- async/await and generators/yield
Stars: ✭ 37 (+37.04%)
Mutual labels:  yield
LuaCSP
Communicating Sequential Processes in Lua
Stars: ✭ 40 (+48.15%)
Mutual labels:  yield
Libgo
Go-style concurrency in C++11
Stars: ✭ 2,521 (+9237.04%)
Mutual labels:  coroutine
rxlife-coroutine
[停止维护]自动管理协程生命周期,并自动捕获异常
Stars: ✭ 27 (+0%)
Mutual labels:  coroutine
Rainlayout
Constraintlayout based rain-animation view developed backed by Kotlin Coroutines.
Stars: ✭ 32 (+18.52%)
Mutual labels:  coroutine
farm-army-backend
Track your farming and pool performance on the Binance Smart Chain, Polygon, Fantom, KuCoin Community Chain, Harmony, Celo - https://farm.army - nodejs backend
Stars: ✭ 86 (+218.52%)
Mutual labels:  yield
delphi
💥 Earn Rewards for Saving and Liquidity Provision (work in progress)
Stars: ✭ 19 (-29.63%)
Mutual labels:  yield
haxe-yield
Cross platform C#-like `yield` generator for Haxe
Stars: ✭ 32 (+18.52%)
Mutual labels:  yield
swoole-examples
Examples on how to use the Swoole async PHP framework
Stars: ✭ 64 (+137.04%)
Mutual labels:  coroutine
Continuables.jl
Extremely fast generator-like alternative to Julia Channels
Stars: ✭ 33 (+22.22%)
Mutual labels:  yield
YACLib
Yet Another Concurrency Library
Stars: ✭ 193 (+614.81%)
Mutual labels:  coroutine
Trip
Async HTTP for Humans, coroutine Requests ⛺️
Stars: ✭ 211 (+681.48%)
Mutual labels:  coroutine
raster
A micro server framework, support coroutine, and parallel-computing, used for building flatbuffers/thrift/protobuf/http protocol service.
Stars: ✭ 19 (-29.63%)
Mutual labels:  coroutine
iiCnma
A playground android app, showcasing the latest technologies and architectures using the Movie Database APIs.
Stars: ✭ 42 (+55.56%)
Mutual labels:  coroutine
hpc
Learning and practice of high performance computing (CUDA, Vulkan, OpenCL, OpenMP, TBB, SSE/AVX, NEON, MPI, coroutines, etc. )
Stars: ✭ 39 (+44.44%)
Mutual labels:  coroutine
redux-saga-callback
redux-saga helper functions to await dispatched actions
Stars: ✭ 19 (-29.63%)
Mutual labels:  yield

PHP Coroutine

A lightweight library to implement coroutine by yield & Generator.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].