Perl6多线程2: Promise new/keep/bread/status/result
来源于个人理解的翻译。
创建一个 promise:
my $p = Promise.new;
可以打印运行 的Promise 状态:
my $p = Promise.new(); $p.then({say 'hello, world'});
say $p.status;
上面的promise创建好后, 当 $p 状态为 kept或broken 时, 会执行 then 里面的 匿名函数。
但是, 上面的$p状态总是为: Plannd。
所以, 那个 hello, world 总是不能打印。
那怎么办呢?
Promise 有两个 函数用于更改 promise状态:
. keep
. break
可以改成这样:
my $p = Promise.new(); $p.then({say 'hello, world'});
say $p.status; $p.keep;
say 'Result ~ '~$p.result;
结果如下:
C:\p6>perl6 scan_dir.p6
Planned
hello, world
Result ~ True C:\p6>
如果把 keep 改为 break的话, 也会让 匿名函数执行, 同时会引发一个常常:
my $p = Promise.new(); $p.then({say 'hello, world'});
say $p.status;
$p.break;
say 'Result ~ '~$p.result;
运行结果如下:
C:\p6>perl6 scan_dir.p6
Planned
hello, world
Tried to get the result of a broken Promise
in block <unit> at scan_dir.p6 line Original exception:
False
in block <unit> at scan_dir.p6 line C:\p6>
注意这里的匿名函数, 当 Promise 执行 kept或 broken 方法时, 会执行它, 最后 promise中的result方法会返回一个值, 这个返回值是传递给 keep 或 break的值或者是then返回了另一个新Promise
的result值(这个值是匿名函数的返回值):
my $p = Promise.new(); $p.then({say 'hello, world'});
say $p.status;
$p.keep('Return for Promise');
say 'Result ~ '~$p.result;
say $p.status;
结果:
C:\p6>perl6 scan_dir.p6
Planned
Result ~ Return for Promise
hello, world
Kept C:\p6>
my $p = Promise.new(); my $p1 = $p.then({ 'hello, world'});
say $p.status;
#$p.keep('Return for Promise');
$p.keep;
say 'Result ~ '~$p.result;
say $p.status;
say $p1.result;
结果:
C:\p6>perl6 scan_dir.p6
Planned
Result ~ True
Kept
hello, world C:\p6>
如果不传递参数执行 $p.keep 时, 匿名函数执行完后, 用 $p.result 会返回一个 True, 这个true 代理 原来的Promise(这里的$p)执行完毕, 状态为 kept。
这个 then 方法, 其实会自动创建一个新的Promise并返回这个Promise(这里的$p1), then方法中的匿名函数参数就是原来的Promise($p), 看如下代码:
my $p = Promise.new(); my $p1 = $p.then(-> $key { say $key.result});
#这里相当于创建了一个新的Promise并返回它
#key 其实就是 $p
$p.keep('This is $p');
#say $p.status;
sleep ;
say $p.status;
say $p1.status;
像一开始所说, Promise.new 创建一个 Promise 时, 默认是不会自动执行的,这时当我们 直接调用 $p.result 想获得 结果时, 程序会一直阻塞(因为我们没有 keep 或 bread 方法告诉这个 promise 让它运行):
my $p = Promise.new(); $p.then({say 'hello, world'});
say $p.status;
say 'Result ~ '~$p.result; #这里会一直阻塞
say 'Done'; #上面被阻塞 , 这里不会运行
那么, 我们能不能一开始创建 一个 promise 后, 让它自动去执行呢?
可以, 用 Promist.start() 方法即可。
最新文章
- AFNetworking 3.0 源码解读(五)之 AFURLSessionManager
- android——从零开始
- Qt 常用的功能
- HTML5学习之跨文档传输消息(七)
- svn提交时强制添加注释
- 如何给一个网卡配置多个虚拟ip
- hdu 5774 Where Amazing Happens
- [Everyday Mathematics]20150122
- Celery Flower监控,完美搞定
- Linq101-Partitioning
- [置顶] 【cocos2d-x入门实战】微信飞机大战之二:别急,先处理好CCScene和CCLayer的关系
- Windows下python安装MySQLdb
- 543. Diameter of Binary Tree
- TypeScript: Week Reflection
- 【bzoj 2916】[Poi1997]Monochromatic Triangles
- network is unreachable 网关PING不通解决办法
- 软件测试中Bug的生命周期以及Bug的严重等级
- Avalon Framework
- 一、spring boot 1.5.4入门(web+freemarker)
- 实现自己的ls命令