The built-in sched module will let you schedule events for the future. (All the code here is Python 3).
import sched
import time
def print_something(x):
print(x)
s = sched.scheduler()
s.enter(1, 0, print_something, ['first'])
s.enter(2, 0, print_something, ['second'])
s.run()
You can provide your own functions to get the current time and to wait until a specified time in the future. The defaults are time.monotonic (a tweaked version of time.time) and time.sleep.
You can get a slight accuracy increase using a busy-wait, like so:
import sched
import time
def print_something(x):
print(x)
def busy_wait(target):
while time.monotonic() < target:
pass
s = sched.scheduler(delayfunc=busy_wait)
s.enter(1, 0, print_something, ['first'])
s.enter(2, 0, print_something, ['second'])
s.run()
But, realistically, there are so many sources of unexpected delay in a desktop OS that if your timing constraints are that tight, you will never get acceptable results without a realtime OS.