jfroebe has asked for the wisdom of the Perl Monks concerning the following question:

Hi again

The stock gethostby* name resolution lookup routines in perl are making blocking function calls. Does any one know of any module that uses the non blocking (reentrant) gethostby*_r function calls? NET::DNS blocks as well :-( This is of particular interest in environments where name resolution can take several minutes to resolve, causing a hang.

The normal catching of the alarm signal does not occur as it is a single op. This forces us at times to use unsafe signal handling which is not at all preferable.

I am curious as to why Perl doesn't use the reentrant gethostby*_r() if they are available... does anyone have any idea why?

Jason L. Froebe

No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Replies are listed 'Best First'.
Re: DNS lookups & signals
by dave_the_m (Monsignor) on May 13, 2004 at 21:21 UTC
    Two things - Perl does use the _r variants if available, but these provide thread-safety rather than async capabilities. Second, Net::DNS does provide a non-blocking variant. From its man page:
    =head2 Perform a background query and do some other work while waiting for the answer. use Net::DNS; my $res = Net::DNS::Resolver->new; my $socket = $res->bgsend("host.example.com"); until ($res->bgisready($socket)) { # do some work here while waiting for the answer # ...and some more here } my $packet = $res->bgread($socket); $packet->print;

      THANKS!

      Now that you point it out, I see it.. :-( Is it time to go home yet?

      Yup, I should have been more explicit. I was thinking of pushing the lookup off into a thread and go do something else. I ended up shooting off another process as the thread hung the main process.

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

        I was thinking of pushing the lookup off into a thread and go do something else

        If by chance that something else would be... yet another DNS lookup, you might be better off using a POE::Component::Client::DNS object and perform many lookups in parallel. This can be a big win, provided you don't particularly care in which order the results come back. That is, if you ask for A, B, C, you might get the results back in the order B, C, A (but in general this is not a problem).

        There's a recipe in the POE cookbook that can help you get started.

        Is it time to go home yet?
        yes, probably ;-)