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

Re: making something happen in real time

by ambrus (Abbot)
on Nov 06, 2005 at 19:35 UTC ( [id://506144]=note: print w/replies, xml ) Need Help??


in reply to making something happen in real time

Before every tick, get the current time, and wait whatever time is left till the next tick.

Here's a simple example.

perl -we 'use Time::HiRes qw"time sleep"; use IO::Handle; $length = 60 +/$ARGV[0]; my $next = time; for (;;) { $next += $length; $now = time; + sleep($next - $now); printflush STDOUT "\a."; }' 80

Of course, this can still be inexact if the load is high (especially if the system is doing io heavily).

On the +%s thing, there's perl -we 'print time() . "\n";'.

Replies are listed 'Best First'.
Re^2: making something happen in real time
by bcrowell2 (Friar) on Nov 06, 2005 at 19:47 UTC
    Before every tick, get the current time, and wait whatever time is left till the next tick.
    That's essentially what I'm already doing.

    Of course, this can still be inexact if the load is high (especially if the system is doing io heavily).
    And that's why it doesn't work :-)

    On the +%s thing, there's perl -we 'print time() . "\n";'
    But I need better resolution than one second.

      But I need better resolution than one second.

      Oh, sorry, didn't listen carefully. Then use Time::HiRes, which is a core module on newer perls:

      perl -we 'use Time::HiRes "time"; print time, "\n";'
      or the gettimeoftheday function from the same module, or (on linux)
      perl -we 'defined(syscall 78, ($d = pack "x100"), 0) or die "panic: ge +ttimeofday failed $!"; ($s, $u) = unpack "l!l!", $d; printf "%d.%06d\ +n", $s, $u;'
      but 156 instead of 78 on solaris, and 116 (untested) instead of 78 on freebsd, (Update:) 96 instead of 78 on linux-x86_64.

      Update:

      Before every tick, get the current time, and wait whatever time is left till the next tick.
      That's essentially what I'm already doing.
      My point is that you have to ask for the time before every sleep. I don't see your code, so you might already be doing that. Of course, if you spawn an external process for querying the time, then that's slow, so don't do that.
        My point is that you have to ask for the time before every sleep. I don't see your code, so you might already be doing that. Of course, if you spawn an external process for querying the time, then that's slow, so don't do that.
        I've switched to using Time::HiRes simply for portability, but it doesn't help with the latency issue at all. Starting a process is fast and predictable. The problem is that regardless of which method I use for sleeping, the latency is very long, and is unpredictable.
        My point is that you have to ask for the time before every sleep. I don't see your code, so you might already be doing that.
        I've posted my code.

        Of course, if you spawn an external process for querying the time, then that's slow, so don't do that.
        Starting up a new process takes a short, predictable time. (The time to start the external "play" utility and start playing the sound is less than ~1 ms, which is negligible for my purposes.) My problem is latency, which is a long and unpredictable time period, often hundreds of milliseconds before my process gets control back from xscreensaver.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found