lis*_*tor 4 identity perl6 promise
我正在尝试编写在promises中运行的3个echo服务器,但我想知道哪个承诺正在进行回显.有没有办法做到这一点?
no strict;
for 0 .. 2 -> $index {
@result[$index] = start {
$myID = $index;
say "======> $myID\n";
my $rsSocket = IO::Socket::INET.new:
localhost => 'localhost',
localport => 1234 + $index,
listen => 1;
while $rsSocket.accept -> $rsConnection {
say "Promise $myID accepted connection";
while $rsConnection.recv -> $stuff {
say "promise $myID Echoing $stuff";
$rsConnection.print($stuff);
}
$rsConnection.close;
}
}
}
await @result;
Run Code Online (Sandbox Code Playgroud)
echo服务器运行正常; 用"nc"测试;
在创建承诺之后,问题$myID就2出现了,并且我无法告诉哪个承诺正在进行当前的回显.它似乎$myID被所有的承诺所使用; 有没有办法创建特定于个人承诺的变量?
这是你通过参加"失败"的事情之一no strict.
你需要的是词法范围.my每次{ ... }输入block()时,Using 将为您提供不同的变量.
如果你这样做:
for 0 .. 2 -> $index {
@result[$index] = start {
my $myID = $index;
Run Code Online (Sandbox Code Playgroud)
然后$myID将是start块的本地,并且每次调用该块时,它将记住它的id.因此,每当套接字接收数据时,您将获得正确的ID.