Giter Site home page Giter Site logo

scheduler's Introduction

scheduler

php基于yiled实现的并行rpc调度器

适用场景

同时请求多个api,而且响应时间比较长,并行化调用是个很好的方案。

版本要求

php >= 5.6

curl扩展

使用示例

并行化调用

include "Scheduler/Autoload.php";
use Scheduler\Scheduler;
use Scheduler\Curl;

$time = microtime(true);

$scheduler = new Scheduler;
/**
 *	第一个参数接受一个迭代生成器
 *  第二个参数接收一个回调函数,会把请求的内容返回
 */
$scheduler->newTask(Curl::request("http://demo.xuanwolei.cn/sleep.php"), function($data, Scheduler $scheduler){
	//输出请求返回内容
	var_dump($data);
});//3秒
$scheduler->newTask(Curl::request("http://www.ali213.net/"));//0.1秒
$scheduler->newTask(Curl::request("http://www.ali213.net/"));//0.1秒
$scheduler->newTask(Curl::request("http://demo.xuanwolei.cn/sleep.php"));//3秒
$scheduler->newTask(Curl::request("http://demo.xuanwolei.cn/sleep.php"));//3秒
//运行
$scheduler->run();

//输出运行时间
echo "run time:".(microtime(true) - $time); //3.1秒

上面的请求并行化调用耗时在3.1秒左右,下面我们看看串行化调用

串行化调用

include "Scheduler/Autoload.php";
use Scheduler\Scheduler;
use Scheduler\Curl;

$time = microtime(true);

//平常的串行调用
$curl = new Curl();
$result = $curl->callWebServer("http://demo.xuanwolei.cn/sleep.php"); //3秒
var_dump($result);
$curl->callWebServer("http://www.ali213.net/"); //0.1秒
$curl->callWebServer("http://www.ali213.net/"); //0.1秒
$curl->callWebServer("http://demo.xuanwolei.cn/sleep.php"); //3秒
$curl->callWebServer("http://demo.xuanwolei.cn/sleep.php"); //3秒

//输出运行时间
echo "run time:".(microtime(true) - $time); //9.3秒

一共耗时9.3秒,可见对于响应时间较长的接口并行化调用带来的提升是巨大的

并行化同时加入生成器

include "Scheduler/Autoload.php";
use Scheduler\Scheduler;
use Scheduler\Curl;

$time = microtime(true);

$scheduler = new Scheduler;
/**
 *  第一个参数接受一个迭代生成器
 *  第二个参数接收一个回调函数,会把请求的内容返回
 */
$scheduler->newTask(Curl::request("http://demo.xuanwolei.cn/sleep.php"), function($data, Scheduler $scheduler){
	//输出请求返回内容
	var_dump($data);
});
$scheduler->newTask(Curl::request("http://www.ali213.net/"));
$scheduler->newTask(Curl::request("http://www.ali213.net/"));
$scheduler->newTask(Curl::request("http://demo.xuanwolei.cn/sleep.php"));
//加入2个生成器
$scheduler->newTask(generator());
$scheduler->newTask(generator());
//运行
$scheduler->run();

//输出运行时间
echo "run time:".(microtime(true) - $time); //3.4秒

/**
 *	生成器:执行完需要1秒
 */
function generator(){
	for ($i=0; $i < 10; $i++) {
		//这里可以是业务逻辑,假设每次需要0.1秒
		usleep(100000);
		yield;
	}
}

加入2个需要运行1秒的生成器后运行时间是3.4秒,比之前多了0.3秒,相当于节省了1.7秒时间。

scheduler's People

Contributors

xuanwolei avatar

Watchers

James Cloos avatar nice172 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.