Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Call function no more than every 0.1 seconds

by Tux (Canon)
on Aug 12, 2020 at 08:49 UTC ( [id://11120647]=note: print w/replies, xml ) Need Help??


in reply to Call function no more than every 0.1 seconds

Using only CORE functionality:

use 5.14.0; use warnings; use Time::HiRes qw( ualarm gettimeofday usleep ); my $delay = 100_000; # us my $x = 0; sub foo { my $duration = rand 1200; # Some will be longer than the max durat +ion my ($sec, $msec) = gettimeofday; printf "%4d %12d.%03d %6d\n", $x++, $sec, int ($msec / 1000), $dur +ation; vec (my $v, 13, 1) = 0; select ($v, $v, $v, $duration / 10000.); } # foo my $running = 0; $SIG{ALRM} = sub { ualarm ($delay); unless ($running) { $running = 1; foo (); $running = 0; } }; ualarm (10); usleep (10) while 1;

-->

$ perl test.pl 0 1597222014.930 430 1 1597222015.030 543 2 1597222015.131 362 3 1597222015.231 1059 4 1597222015.337 929 5 1597222015.437 611 6 1597222015.537 1027 7 1597222015.640 396 8 1597222015.740 243 9 1597222015.840 1095 10 1597222015.950 710 11 1597222016.050 674 12 1597222016.150 695 13 1597222016.250 842 14 1597222016.350 901 15 1597222016.450 468 16 1597222016.550 568 17 1597222016.650 322 18 1597222016.750 633 19 1597222016.850 194 20 1597222016.950 1141 21 1597222017.064 365 22 1597222017.165 226 23 1597222017.265 1162 24 1597222017.381 998 25 1597222017.481 543 26 1597222017.581 184 27 1597222017.681 197 28 1597222017.781 431 29 1597222017.881 1081 30 1597222017.990 644 31 1597222018.090 49 32 1597222018.190 500 33 1597222018.290 1137 34 1597222018.404 171 35 1597222018.504 995 36 1597222018.604 847 37 1597222018.704 998 38 1597222018.804 262 39 1597222018.904 1043 40 1597222019.009 292 41 1597222019.109 668 42 1597222019.209 805 43 1597222019.309 886 44 1597222019.409 361 45 1597222019.509 676 ^C

Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

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

    No recent polls found