Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Is it possible to issue a perl command to sleep for less than 1 second?

by wruehl (Acolyte)
on Aug 24, 2007 at 14:41 UTC ( [id://634876]=perlquestion: print w/replies, xml ) Need Help??

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

I'm writing a polling script that hits the same port twice to get the difference in traffic data. Just running the snmp sessions right after each other is ineffective as it is too fast. I'd like to run sleep to delay the next grab, but I don't want to sleep for the whole second, as it would slow the execution time of the script greatly, making it take 3 to 4 times as long to finish. I'd like to try and make it sleep for .1 or .2 seconds.

Can anyone tell me how to do this?

Thanks

  • Comment on Is it possible to issue a perl command to sleep for less than 1 second?

Replies are listed 'Best First'.
Re: Is it possible to issue a perl command to sleep for less than 1 second?
by FunkyMonk (Chancellor) on Aug 24, 2007 at 14:43 UTC
    Time::HiRes allows you to sleep for fractions of a second.

Re: Is it possible to issue a perl command to sleep for less than 1 second?
by zentara (Archbishop) on Aug 24, 2007 at 14:49 UTC
    select(undef,undef,undef, .1); #100 millisecond delay

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      Yes, that is a simple method. See select, where it states:

      You can effect a sleep of 250 milliseconds this way:

      select(undef, undef, undef, 0.25);

      Dave

      I'd have never thought up this usage of select. Is it a trick used for fractional second sleeps from before Time:HiRes existed?

        It's been around forever(since C was created). Most people discover it when first doing a "perldoc -q sleep" and are referred to "perldoc -f select". Ultimately it is based on the c code, where select can be used as a delay if the file descriptors are left undefined. Read "man select" if you want to know how it's magic works.

        Now that you know, you will probably recognize it in alot of code. I usually put an inline comment when I use it, signifying it is used for a delay, so the uninitiated will get it.


        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Is it possible to issue a perl command to sleep for less than 1 second?
by haoess (Curate) on Aug 24, 2007 at 14:48 UTC

    You can always use perlfaq for this kind of questions:

    $ perldoc -q sleep

    -- Frank

Re: Is it possible to issue a perl command to sleep for less than 1 second?
by kscript (Novice) on Aug 24, 2007 at 15:09 UTC
    You can use Time::HiRes for that (as stated above).
    use Time::HiRes qw (sleep); sleep (0.15); # Replaces sleep() with a high-precision alternative Time::HiRes::sleep (0.2); # if you don't want your standard sleep() ov +erwritten
Re: Is it possible to issue a perl command to sleep for less than 1 second?
by 23skiddoo (Beadle) on Aug 28, 2007 at 13:52 UTC
    I think you can use the Time::HiRes module to get what you want. It comes with the usleep function which will take microseconds. See Recipe 3.9 of the Perl Cookbook. Cheers!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://634876]
Approved by dsheroh
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found