问题描述
在Raku文档中,有关 Supply 方法动作(与点击相比) https://docs.raku.org/type/Supply#method_act指出:
我的理解是,一个线程必须先运行特定的代码对象,然后才能运行另一个线程。
如果是这种情况,当我尝试实现该功能时,我偶然发现了另一种行为。看一下下面的代码片段,其中创建了两个“ act”并在不同的线程中运行:
#!/usr/bin/env perl6
say 'Main runs in [ thread : ',+$*THREAD,' ]';
my $b = 1;
sub actor {
print " Tap_$*tap : $^a ",Now;
$*tap < 2 ??
do {
say " - Sleep 0.1";
sleep 0.1
}
!!
do {
say " - Sleep 0.2";
sleep 0.2;
}
$b++;
say " Tap_$*tap +1 to \$b $b ",Now;
}
my $supply = supply {
for 1..100 -> $i {
say "For Tap_$*tap [ \$i = $i ] => About to emit : $b ",Now;
emit $b;
say "For Tap_$*tap [ \$i = $i ] => Emitted : $b ",Now;
done if $b > 5
}
}
start {
my $*tap = 1;
once say "Tap_$*tap runs in [ thread : {+$*THREAD} ]";
$supply.act: &actor
}
start {
my $*tap = 2;
once say "Tap_$*tap runs in [ thread : {+$*THREAD} ]";
$supply.act: &actor
}
sleep 1;
结果如下(带有额外的时间间隔和注释):
1 Main runs in [ thread : 1 ] - Main thread
2 Tap_1 runs in [ thread : 4 ] - Tap 1 thread
3 For Tap_1 [ $i = 1 ] => About to emit : 1 Instant:1603354571.198187 - Supply thread [for tap 1]
4 Tap_1 : 1 Instant:1603354571.203074 - Sleep 0.1 - Tap 1 thread
5 Tap_2 runs in [ thread : 6 ] - Tap 2 thread
6 For Tap_2 [ $i = 1 ] => About to emit : 1 Instant:1603354571.213826 - Supply thread [for tap 2]
7 Tap_2 : 1 Instant:1603354571.213826 - Sleep 0.2 - Tap 2 thread
8
9 -----------------------------------------------------------------------------------> Time +0.1 seconds
10
11 Tap_1 +1 to $b 2 Instant:1603354571.305723 - Tap 1 thread
12 For Tap_1 [ $i = 1 ] => Emitted : 2 Instant:1603354571.305723 - Supply thread [for tap 1]
13 For Tap_1 [ $i = 2 ] => About to emit : 2 Instant:1603354571.30768 - Supply thread [for tap 1]
14 Tap_1 : 2 Instant:1603354571.30768 - Sleep 0.1 - Tap 1 thread
15
16 -----------------------------------------------------------------------------------> Time +0.1 seconds
17
18 Tap_1 +1 to $b 3 Instant:1603354571.410354 - Tap 1 thread
19 For Tap_1 [ $i = 2 ] => Emitted : 4 Instant:1603354571.425018 - Supply thread [for tap 1]
20 Tap_2 +1 to $b 4 Instant:1603354571.425018 - Tap 2 thread
21 For Tap_1 [ $i = 3 ] => About to emit : 4 Instant:1603354571.425018 - Supply thread [for tap 1]
22 For Tap_2 [ $i = 1 ] => Emitted : 4 Instant:1603354571.425995 - Supply thread [for tap 2]
23 Tap_1 : 4 Instant:1603354571.425995 - Sleep 0.1 - Tap 1 thread
24 For Tap_2 [ $i = 2 ] => About to emit : 4 Instant:1603354571.425995 - Supply thread [for tap 2]
25 Tap_2 : 4 Instant:1603354571.426973 - Sleep 0.2 - Tap 2 thread
26
27 -----------------------------------------------------------------------------------> Time +0.1 seconds
28
29 Tap_1 +1 to $b 5 Instant:1603354571.528079 - Tap 1 thread
30 For Tap_1 [ $i = 3 ] => Emitted : 5 Instant:1603354571.52906 - Supply thread [for tap 1]
31 For Tap_1 [ $i = 4 ] => About to emit : 5 Instant:1603354571.52906 - Supply thread [for tap 1]
32 Tap_1 : 5 Instant:1603354571.53004 - Sleep 0.1 - Tap 1 thread
33
34 -----------------------------------------------------------------------------------> Time +0.1 seconds
35
36 Tap_2 +1 to $b 6 Instant:1603354571.62859 - Tap 2 thread
37 For Tap_2 [ $i = 2 ] => Emitted : 6 Instant:1603354571.62859 - Supply thread [for tap 2]
38 Tap_1 +1 to $b 7 Instant:1603354571.631512 - Tap 1 thread
39 For Tap_1 [ $i = 4 ] => Emitted : 7 Instant:1603354571.631512 - Supply thread [for tap 2]
一个人可以轻松地观察到代码对象(子例程&actor )正在2个线程中同时运行(例如,参见输出行4和7)。
有人可以澄清我对此事的误解吗?
解决方法
在日常使用Raku的过程中,tap
和act
之间几乎没有什么区别,因为您遇到的几乎每个Supply
都是串行供应。串行供应是已经强制执行以下协议的协议:在处理之前的一个之前,不会发出值。 act
的实现是:
method act(Supply:D: &actor,*%others) {
self.sanitize.tap(&actor,|%others)
}
sanitize
在哪里强制执行值的连续发射,另外还要确保事件遵循语法emit* [done | quit]
。由于无论如何通常都非常需要这些属性,因此,除了能够创建Supply
并在其上调用Supplier
以外,每种获取unsanitized-supply
的内置方式都可以提供它们。 (历史记录:一个非常早期的原型并没有那么广泛地强制执行这些属性,这使得人们对实现act
的方法的需求更加迫切。而随着设计涉及最终交付给客户的东西,对它的需求有所减少。的第一个语言版本,它必须保持其漂亮的简称。)
误解是由于期望事件的序列化是每个源,而实际上是每个订阅。考虑以下示例:
my $timer = Supply.interval(1);
$timer.tap: { say "A: {now}" };
$timer.tap: { say "B: {now}" };
sleep 5;
哪个会产生如下输出:
A: Instant:1603364746.02766
B: Instant:1603364746.031255
A: Instant:1603364747.025255
B: Instant:1603364747.028305
A: Instant:1603364748.025584
B: Instant:1603364748.029797
A: Instant:1603364749.026596
B: Instant:1603364749.029643
A: Instant:1603364750.027881
B: Instant:1603364750.030851
A: Instant:1603364751.030137
有一个事件源,但我们为此建立了两个订阅。每个订阅都将执行串行规则,因此,如果我们这样做:
my $timer = Supply.interval(1);
$timer.tap: { sleep 1.5; say "A: {now}" };
$timer.tap: { sleep 1.5; say "B: {now}" };
sleep 5;
然后我们观察到以下输出:
A: Instant:1603364909.442341
B: Instant:1603364909.481506
A: Instant:1603364910.950359
B: Instant:1603364910.982771
A: Instant:1603364912.451916
B: Instant:1603364912.485064
显示每个订阅一次获取一个事件,但是仅共享(按需)源并不会产生任何共享的背压。
由于并发控制与 subscription 相关联,因此是否将相同的闭包克隆传递给tap
/ act
无关紧要。跨多个订阅实施并发控制是supply
/ react
/ whenever
的领域。例如:
my $timer = Supply.interval(1);
react {
whenever $timer {
sleep 1.5;
say "A: {now}"
}
whenever $timer {
sleep 1.5;
say "B: {now}"
}
}
给出如下输出:
A: Instant:1603365363.872672
B: Instant:1603365365.379991
A: Instant:1603365366.882114
B: Instant:1603365368.383392
A: Instant:1603365369.884608
B: Instant:1603365371.386087
由于react
块暗含的并发控制,每个事件相隔1.5s。