前言
第一部分: 半协程调度器
- 统一生成器接口
- 生成器迭代
- 生成器返回值
- 生成器委托
- 改写 return
- 抽象异步模型
- 引入异常处理
- 异常: 嵌套任务透传
- 异常: 传递流程
- 异常: 重新进行 CPS 变换
- 异常: 重新加入 Async
- Syscall 与 Context
- 调度器: 里程碑
- spawn
- callcc
- race 与 timeout
- all 与 parallel
- channel 与协程间通信
- 无缓存 channel
- 缓存 channel
- channel 演示
- FutureTask 与 fork
第二部分: Koa
- 穿越地心之旅
- 洋葱圈模型
- rightReduce与中间件compose
- Koa::Application
- Koa::Context
- Koa::Request
- Koa::Response
- Koa - HelloWorld
- Middleware Interface
- Middleware: 全局异常处理
- Middleware: Router
- Middleware: 请求超时
- 一个综合示例
附录
参考
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
all 与 parallel
all与parallel
Any表示多个异步回调,任一回调完成则任务完成,All表示等待所有回调均执行完成才算任务完成,二者相同点是IO部分并发执行;
<?php
class All implements Async
{
public $parent;
public $tasks;
public $continuation;
public $n;
public $results;
public $done;
public function __construct(array $tasks, AsyncTask $parent = null)
{
$this->tasks = $tasks;
$this->parent = $parent;
$this->n = count($tasks);
assert($this->n > 0);
$this->results = [];
}
public function begin(callable $continuation = null)
{
$this->continuation = $continuation;
foreach ($this->tasks as $id => $task) {
(new AsyncTask($task, $this->parent))->begin($this->continuation($id));
};
}
private function continuation($id)
{
return function($r, $ex = null) use($id) {
if ($this->done) {
return;
}
// 任一回调发生异常,终止任务
if ($ex) {
$this->done = true;
$k = $this->continuation;
$k(null, $ex);
return;
}
$this->results[$id] = $r;
if (--$this->n === 0) {
// 所有回调完成,终止任务
$this->done = true;
if ($this->continuation) {
$k = $this->continuation;
$k($this->results);
}
}
};
}
}
function all(array $tasks)
{
$tasks = array_map(__NAMESPACE__ . "\\await", $tasks);
return new Syscall(function(AsyncTask $parent) use($tasks) {
if (empty($tasks)) {
return null;
} else {
return new All($tasks, $parent);
}
});
}
<?php
spawn(function() {
$ex = null;
try {
$r = (yield all([
async_dns_lookup("www.bing.com", 100),
async_dns_lookup("www.so.com", 100),
async_dns_lookup("www.baidu.com", 100),
]));
var_dump($r);
/*
array(3) {
[0]=>
string(14) "202.89.233.103"
[1]=>
string(14) "125.88.193.243"
[2]=>
string(15) "115.239.211.112"
}
*/
} catch (\Exception $ex) {
echo $ex;
}
});
我们这里实现了与Promise.all相同语义的接口,或者更复杂一些,我们也可以实现批量任务以chunk方式进行作业的接口,留待读者自己完成;
至此, 我们已经拥有了 spawn
callcc
race
all
timeout
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论