使用Proc :: Async从绑定管道读取

jjm*_*elo 8 asynchronous stdout pipe process perl6

Proc::Async是Perl 6用于与系统异步交互的类之一.文档指定了这种绑定到外部程序输出的方式:

my $p = Proc::Async.new("ls", :out);
my $h = "ls.out".IO.open(:w);
$p.bind-stdout($h);
await $p.start;

say "Done";
Run Code Online (Sandbox Code Playgroud)

(添加了一些修改,比如等待承诺).但是,我不知道如何打印输出$p.添加tap产生此错误:

Cannot both bind stdout to a handle and also get the stdout Supply
Run Code Online (Sandbox Code Playgroud)

在bind-stdout.p6第8行的块中

文档中有print写入和写入方法,但read除了阅读文件之外,我不知道如何使用它.任何的想法?

nxa*_*adm 8

我不确定你能做到这一点(错误很明确).作为一种解决方法,您可以定期点击并打印到stdout和同一块中的文件:

my $p = Proc::Async.new("ls", :out);
my $h = "ls.out".IO.open(:w);
$p.stdout.tap(-> $str { print $str; $h.print($str) });
await $p.start;

say "Done";
Run Code Online (Sandbox Code Playgroud)

  • 是的,这是正确的.您可以通过获取"Supply"来绑定句柄或从中读取.我们只能将一个东西绑定到目标进程标准句柄,因此这里的例外只是反映了Perl 6无法真正做多少的低级别限制. (4认同)