因此,我试图理解基于事件和基于并行的风格的差异.我读了关于How to articulate the difference between asynchronous and parallel programming?的问题,但无论如何我很困惑.
为什么它对我来说非常重要:
fork() or its analog is a heavy weapon,as I know. It copy data (or
share it,but in some case it worst),spawn new process (which may die
or something bad happened),have huge problem to return data back to
“parent” process and so on. Yes,at CPAN is exists bunch of good
designed modules to wrap this problem and give to me smooth interface,
but any way fork-style have some overhead.In this case,as I see,I need follow to simply rule – “if you CAN solve you
problem without fork() or Parallel::* you MUST NOT use it,you
SHOULD use event-based solutions instead“May be I totally wrong there too?
是不是我看到了:
>如果我们有IO操作或其他东西,使用外部(对我的程序)事件,如信号,文件更改,或管道中的数据等,应选择基于事件的样式
>如果我们只有内部(对程序)“事件”,就像子程序完成计算一样,必须选择基于并行的样式.
换句话说,如果我总是有子程序或类方法/功能,我知道它有一些延迟“内部”(因为很多计算或它等待的东西 – 没有区别) – 我必须使用基于并行的风格只要.并且无法使用基于事件的样式,它会不断地工作,因为此代码将阻止主程序并阻止“事件流”.
并且我可以使用基于事件的样式,只有当我在子程序或类方法/函数中写入“等待延迟时间”时,它调用主程序之外的东西.
这是正确的吗?
编辑:
这是清除伪代码
# we are CANT re-write this methods,but we are known it`s have latency sub method_with_latency_as_blackBox1{...}; sub method_with_latency_as_blackBox2{...}; my @in_data = (2,3,5); my @out_data; # we are need to run this for each item in @in_data array,somehow # and there we are CAN re-write code to speed up it # by somehow "separating" all block execution for each item # and not method_1* from method_2* - it`s impossible because it related foreach my $ent ( @in_data ){ my $res1 = method_with_latency_as_blackBox1( $ent ); my $res2 = method_with_latency_as_blackBox2( $ent,$res1 ); # yes,it related push @out_data,$res2; # we are not care about result position }
我如何使用AnyEvent或Coro来加速像这样的代码?