Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Parallel DNS queries (use threads; )

by BrowserUk (Pope)
on Aug 05, 2004 at 17:10 UTC ( #380330=note: print w/replies, xml ) Need Help??


in reply to Parallel DNS queries -- please comment on code

TIMTOWTDI --

#! perl -slw use strict; use threads qw[ async ]; use Thread::Queue; my @dnss = qw( 111.111.111.111 222.222.222.222 111.222.111.222 222.111.222.111 ); my $Q = new Thread::Queue; my $host = $ARGV[ 0 ]; my $count : shared = @dnss; async{ $Q->enqueue( qx[ nslookup -type=A $host $_ 2>nul ] ); $count--; } for @dnss; sleep 1 while $count; ## Do something with results print $Q->dequeue while $Q->pending;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Parallel DNS queries (use threads; )
by nothingmuch (Priest) on Aug 06, 2004 at 12:19 UTC
    The semaphores provided with perl's threading modules should provide a nicer solution than sleep 1 while $count.
    -nuffin
    zz zZ Z Z #!perl

      Why write 30 lines of complicated code when one is just about perfect.

      sleep causes the sleeping thread to relinguish its timeslice(s) until the sleep times out, which means almost no load.

      The semaphores api (like the rest of the pthreads api that perls threads are modelled on), is under specified, messy and hard to get right.

      The sleep 1 while $count; is easy to code, easy to understand and "just works" :)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        The one reason i wouldn't use it is because it's limited to 1 second granuality. But i'm one of those damned perfectionist. As for the API, since 5.8.0 there is one way to do it - nothing shared, and I don't like my threads like that. So i don't remember what it's like, but that's a good reason not to use them in the example.

        For the record, the main reason I commented was in the hopes that you add a

        sleep 1 while $count; # Or use Thread::Semaphore
        so that people who don't know about threads wouldn't think that's the only way. I guess I should have been more polite or something... =)

        -nuffin
        zz zZ Z Z #!perl

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2021-09-22 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?