Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Parallel::ForkManager (high cpu and a lot of memory)

by marto9 (Beadle)
on Oct 09, 2008 at 14:38 UTC ( [id://716223]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Parallel::ForkManager (high cpu and a lot of memory)
in thread Parallel::ForkManager (high cpu and a lot of memory)

Thx for all the help so far!

But I still have a question in my mind.
If I would code thesame in c++ for example. Would the process use less memory and cpu?

  • Comment on Re^4: Parallel::ForkManager (high cpu and a lot of memory)

Replies are listed 'Best First'.
Re^5: Parallel::ForkManager (high cpu and a lot of memory)
by BrowserUk (Patriarch) on Oct 09, 2008 at 15:27 UTC

    It depends. If you used MFC or STL probably not. If you use ATL maybe. If you eshew the use of template libraries altogether and write directly to the API (Win32 or POSIX) probably.

    But the other side of that equation is instead of 15 or 20 lines of code you'll end up writing 100s. And they will be much harder to get right, and much, much harder to debug when they go wrong.

    You could also substantially reduce the memory consumption of the Perl program by avoiding LWP and writing your own socket code. For what you are doing it wouldn't be very complex, but it would be much harder to get right.

    Or you can strike a compromise and use LWP::Simple and a pool of threads. This version uses ~20MB and < 3% cpu for 10 threads on my system:

    #! perl -slw use strict; use threads; use LWP::Simple; use threads::shared; use Thread::Queue; $|++; our $THREADS ||= 10; my $sem :shared; my $Q = new Thread::Queue; sub worker { while( my $url = $Q->dequeue ) { chomp( $url ); { lock $sem; print "fetching $url" } my $content = get "http://$url/"; lock $sem; print "$url : ", $content && $content =~ /ok/ ? 'ack' : 'nack' + } } $Q->enqueue( <> ); $Q->enqueue( (undef) x $THREADS ); my @threads = map threads->create( \&worker ), 1 .. $THREADS; $_->join for @threads __END__ C:\test>715836 urls.txt fetching www.bbc.co.uk fetching news.bbc.co.uk fetching www.ibm.com fetching www.yahoo.com fetching www.microsoft.com fetching www.google.com fetching www.google.co.uk fetching www.google.co.au fetching www.cnn.com fetching www.msnbc.com www.google.co.au : nack fetching www.perl.org www.google.co.uk : ack fetching www.nasa.com www.yahoo.com : nack fetching www.ask.com www.google.com : ack fetching www.itv.com www.perl.org : nack www.nasa.com : nack www.ask.com : nack www.msnbc.com : ack www.itv.com : nack www.ibm.com : ack www.microsoft.com : ack news.bbc.co.uk : ack www.cnn.com : ack www.bbc.co.uk : ack

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found