4

It`s slightly difficult to ask good questions if you knowledge a small, but I try to be best. My apologies if I mislead you.

So, I try to understand generally differences on event-based and parallel-based style. I read question about How to articulate the difference between asynchronous and parallel programming?, but anyway I confused.

Why it is very important to me:

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?

Is it right I see:

  • event-based style should be chosen if we are have IO-operations or something, using external (to my program) event, like signal, or file changed, or data in pipe and so on

  • parallel-based style must be chosen if we are have only internal (to the program) "event", like subroutine finish calculated.

In other word, if I always have subroutine or class method/function and I know it`s have some latency "inside" ( because a lot of calculation or its waiting something - the are no differences ) - I must use parallel-based style only. And no way to use it event-based style, it be worked continuously, because this code will block main program and will block "event flow".

And I may use event-based style only if I write subroutine or class method/function with "waiting latency inside", when it calling something outside main program.

Is it correctly?

EDIT:

this is pseudo-code for clearance

# 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
}

how I can use AnyEvent or Coro to speed up code like this one?

Community
  • 1
  • 1
Meettya
  • 345
  • 2
  • 9
  • What problem I trying to solve - I want understund clear how improve "old continuously" code.Imagine - exists some main program and some module, which method have latency. This method (huge & complex) used in main program (fe. for each values of array) and cant be re-factored now (its "black box"). How I can speed up main program, running latency method for array values? May I use event-based (AnyEvent) style there, or it must be parallel-based style only? – Meettya Jul 27 '11 at 22:30
  • 1
    Threads & new processes lets you run stuff in parallel (more than one thing at once). Event based/asynchronous/non-blocking lets you start something and return before it's finished. Both can be used to do "many things at once" and hence speed things up. But, you have to be sure that the things you are trying to compute does not affect each other or depend on each other. If they do, doing them in parallel can get tricky. – Øyvind Skaar Jul 28 '11 at 08:26
  • @Øyvind Skaar I add pseudo-code example, can you direct me? – Meettya Jul 28 '11 at 10:50
  • 1
    as long as method_*2 does not depend on the results from method*1, I guess I would just fork(). Google & SO are your friends =) http://search.cpan.org/~dlux/Parallel-ForkManager-0.7.9/lib/Parallel/ForkManager.pm#Parallel_get http://en.wikipedia.org/wiki/Fork_%28operating_system%29 http://perldoc.perl.org/functions/fork.html – Øyvind Skaar Jul 28 '11 at 12:01
  • http://stackoverflow.com/questions/2510306/how-can-i-manage-a-fork-pool-in-perl – Øyvind Skaar Jul 28 '11 at 12:02
  • You sure have a lot of different people helping you, and you still have "0 votes cast" and "0 accepted answers?" ? That's just mean >( – Øyvind Skaar Jul 28 '11 at 12:05
  • my English is so bad, my apologies. I try to get answer how it possible to speed up _all block_ (method_1 **and** method_2), "separating" execution for each pice of data in @data_in, not separate method_1 from method_2. I think only parallel-based style (like fork() or something closer) can solve this problem, not AnyEvent nor Core. But may be I dont seen something in AE, and AE CAN do this? – Meettya Jul 28 '11 at 13:13
  • @Øyvind Skaar - I go to read about "0 votes cast" and "0 accepted answers?". May be I`m just not understand it? I`m not evil, just little dump :) – Meettya Jul 28 '11 at 13:15
  • Other people (like me ;) get reputation when you vote theirquestion/answer up, or accept an answer. See: http://stackoverflow.com/faq#reputation – Øyvind Skaar Jul 28 '11 at 15:27

1 Answers1

1

No, you can use whatever you want. I Disagree with the notion of "inside" latency, there is always latency between different computer "parts" and it's "all" outside your program. But network input/output is extremely slow compared to disk io (which is extremely slow compared to memory/ram, etc..).

The top answer on the SO question you linked offers a good explanation.

If you want a better answer you'll have to tell us what you are creating or what problem you are trying to solve :)

Edit:

To try point you in the right direction:

I have hear that in perl you should either just fork new processes, use Coro or use something event based like AnyEvent. POE (see what is POE?) could also be what you want, but it's complex and not something you learn in five minutes.

Øyvind Skaar
  • 2,278
  • 15
  • 15
  • about latency - its depends, fe: we are have method, its get large text from arguments and analyse it, then return some results. Not network or disk IO exists. Yes, it work at memory, but we are not manage memory IO at perl. – Meettya Jul 27 '11 at 22:16