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状态:

1. keep
2. 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 12

Original exception:
    False
      in block <unit> at scan_dir.p6 line 12


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 1;
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() 方法即可。

原文地址:https://www.cnblogs.com/perl6/p/7440320.html