Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Don't ask to ask, just ask
 
PerlMonks  

Re: while reading a file, transfer the same data to two different processes.

by weismat (Friar)
on May 20, 2010 at 16:46 UTC ( #840980=note: print w/ replies, xml ) Need Help??


in reply to while reading a file, transfer the same data to two different processes.

I would work with threads and use two shared queues to send the data to the threads.
Every fork has a rather big overhead.


Comment on Re: while reading a file, transfer the same data to two different processes.
Re^2: while reading a file, transfer the same data to two different processes.
by zwon (Monsignor) on May 20, 2010 at 18:16 UTC

    Well, every thread has some overhead too:

    use strict; use warnings; use threads; my $str = 'x' x 32_000_000; sub fork_wait { my $pid = fork; if ($pid) { wait; } else { exit 0; } } sub create_thread_join { threads->create( sub { } )->join; } use Benchmark qw( cmpthese ); cmpthese - 3, { Forks => \&fork_wait, Threads => \&create_thread_join, }; __END__ Rate Threads Forks Threads 27.7/s -- -73% Forks 103/s 272% --

    Even without this 32M string threads not win. This is Linux, perhaps on Windows you will get other result.

      perhaps on Windows you will get other result.

      On windows, those two pieces of code are all but identical (except the fork version leaks scalars). Hence, there is little difference between them. I'd be interested to see the results without the 32M scalar if you've a moment?

        Without 32M scalar it outputs:

        Rate Threads Forks Threads 230/s -- -0% Forks 231/s 0% --
Re^2: while reading a file, transfer the same data to two different processes.
by ikegami (Pope) on May 20, 2010 at 20:51 UTC
    His reason for using fork isn't parallelisation, it's crash-resistance. Threads won't help at all.
      Shouldn't he be using eval in that case?
        To know what he should use would require knowing his needs, and I don't know them. eval offers far less protection than forking. eval catches exceptions. Forking catches exceptions, signals and faults. It also provide a fresh environment which clears leaks and corrupt structures.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://840980]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-04-21 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls