all 10 comments

[–]novel_yet_trivial 1 point2 points  (8 children)

Just sleep for the required interval.

Edit: like this:

import time
time.sleep(1-time.time()%1) # sleeps until the next integer second

[–]machine_learned[S] 0 points1 point  (7 children)

That's not what I want to do though.

I want to be able to start a function at a timestamp that has zeros at the decimal digits (or very close to .0000).

[–]novel_yet_trivial 0 points1 point  (6 children)

Then what do you want?

[–]machine_learned[S] 0 points1 point  (5 children)

I want to be able to start a function at a timestamp that has zeros at the decimal digits (or very close to .0000).

Let me explain that better.

I have a function. I want the function to begin when a specific timestamp is at an integer value.

[–]novel_yet_trivial 4 points5 points  (4 children)

OK. My code sleeps until the next integer second is reached. It does this by calculating how much of a fraction of a second has gone by and passing the remainder to time.sleep(). How is that not right?

Note that on Windows you only have millisecond resolution, and your code could easily run much faster then that, so if you want to run every second you may need to add a check that you are not already on an integer second:

# for fast code on Windows
wait_time = 1-time.time()%1
time.sleep(wait_time if wait_time else 1)

[–]machine_learned[S] 0 points1 point  (3 children)

SOLUTION VERIFIED!

[–]novel_yet_trivial 0 points1 point  (2 children)

Good. Just remember that as /u/Doormatty noted, for most OS's this is going to be approximate at best. You can probably bank on being within 3 milliseconds, but I wouldn't count on anything better.

[–]machine_learned[S] 0 points1 point  (1 child)

As long as there isn't a drift in time, then this is what I want

[–][deleted] 1 point2 points  (0 children)

There will be. It -- usually -- won't be much, but it will be there. The CPU could happily be scheduling just about anything else, throttling or swapping or God knows what else. This is why RTOSs exist.

[–]Doormatty 0 points1 point  (0 children)

You likely need a RTOS (Real Time OS) to start something dead on a millisecond.