0

I am new to Python so be gentle.

I have been trying to write a program that counts or measures events in real time. At the moment I am using the sleep command but this pause event doesn't take into account the time the program takes to run. I have read up on the datetime module and can sort of see how this could be used, but I am a bit stuck in implementing this.

In short, I want a program that counts from 0 to 100 in real time seconds and milliseconds.

john_science
  • 6,325
  • 6
  • 43
  • 60
  • 2
    This question might be closed. http://mattgemmell.com/2008/12/08/what-have-you-tried/ – zengr Sep 03 '13 at 19:56
  • 1
    http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy – Pol0nium Sep 03 '13 at 19:57
  • You might take a look at [this question](http://stackoverflow.com/questions/8388967/limiting-fps-in-python) about frame rate limiting. Basically, you need to consider that the sleep time can be variable. – thegrinner Sep 03 '13 at 20:09
  • What kind of real time events are you trying to measure? – Keith Sep 03 '13 at 20:34

1 Answers1

3

Your best bet (besides Googling for help before posting a question on SO) might be to do something like this:

  • Note the time when the program starts. start = datetime.datetime.now()
  • Do your calculations
  • sleep until 100 seconds after start. (start + datetime.timedelta(seconds=100))
    • Note that this won't be perfect, since there is a little overhead involved with the steps between accessing the "current time" and going to "sleep" (e.g. subtracting "current time" from "wake-up time"). However, if your sleep precision only needs to be in seconds, you should be okay.
  • Repeat as needed.

If, after trying it out, you need additional help with the actual implementation of these steps, feel free to come back and post another question on that topic.

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80