我想知道子流程在 Mojolicious 中是如何工作的。
这是我正在运行的代码:
use Mojo::Base -strict, -signatures;
use Mojo::IOLoop::Subprocess;
show "BEFORE";
for(0..5){
say "(START) This is $_ loop";
my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
map {
sub {
my $sp = shift; # $subprocess
my $i = shift; # looking to get my param here
say "Hello, from $i!";
sleep 5;
say "Good bye, from $i!";
return 'Done', $i;
},
sub ($subprocess, $err, @results) {
say "Subprocess error: $err" and return if $err;
say "$results[0] SP: $results[1]!";
}
} $_ # assuming to send the $_ from the for() loop
);
$subprocess->ioloop->start unless $subprocess->ioloop->is_running;
say "(END) This is $_ loop";
}
show "AFTER";
这是回应:
======( "BEFORE" )===========[ 'Test.pm', line 168 ]======
"BEFORE"
(START) This is 0 loop
(END) This is 0 loop
(START) This is 1 loop
(END) This is 1 loop
(START) This is 2 loop
(END) This is 2 loop
(START) This is 3 loop
(END) This is 3 loop
(START) This is 4 loop
(END) This is 4 loop
(START) This is 5 loop
(END) This is 5 loop
======( "AFTER" )============[ 'Test.pm', line 197 ]======
"AFTER"
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 181.
Hello, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 181.
Hello, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 181.
Hello, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 181.
Hello, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 181.
Hello, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 181.
Hello, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 183.
Good bye, from !
Use of uninitialized value $results[1] in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 188.
Done SP: !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 183.
Good bye, from !
Use of uninitialized value $results[1] in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 188.
Done SP: !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 183.
Good bye, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 183.
Good bye, from !
Use of uninitialized value $results[1] in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 188.
Done SP: !
Use of uninitialized value $results[1] in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 188.
Done SP: !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 183.
Good bye, from !
Use of uninitialized value $i in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 183.
Good bye, from !
Use of uninitialized value $results[1] in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 188.
Done SP: !
Use of uninitialized value $results[1] in concatenation (.) or string at /home/sites/somesite/myapp/script/../lib/MyApp/Controller/Test.pm line 188.
Done SP: !
我这里有几个问题:
"AFTER"
字符串必须在所有子进程完成后运行)。我希望通过循环运行多个子进程,并需要它们以 parallel 的方式工作并阻塞主程序,直到所有程序都完成(这个想法就像 Promise 中的 all() 一样,但在每个子进程中接受阻塞代码子进程,这不会阻止其他子进程并行运行)。$subprocess
.做了什么: 我花了几个小时研究 Future async/await、Mojo async/await、Promise 等等,但是如果你有阻塞代码(例如,
sleep()
或 perl 内置循环),它就不起作用,这是可以预见的.
子流程使用不同的流程(从
$$
可以看出,如果我从子流程中打印出来,它总是为每次迭代提供新的价值)。但我找不到办法 - 如何摆脱它所描述的问题。
感谢任何帮助!