Skip to content

Instantly share code, notes, and snippets.

@njsmith
Created June 6, 2019 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njsmith/f91b8c1e92eadb5e6dd98608c2374911 to your computer and use it in GitHub Desktop.
Save njsmith/f91b8c1e92eadb5e6dd98608c2374911 to your computer and use it in GitHub Desktop.
~/trio/notes-to-self$ python sleep-time.py
----------------------------------------------------------------
Starting: import select; select.select([], [], [], 6)
Expected duration: 6 seconds
Putting it to sleep for 2.0 seconds
Waking it up again
Actual duration: 8.05
----------------------------------------------------------------
Starting: import select; ep = select.epoll(); ep.poll(6)
Expected duration: 6 seconds
Putting it to sleep for 2.0 seconds
Waking it up again
Actual duration: 6.06
----------------------------------------------------------------
Starting: import threading; ev = threading.Event(); ev.wait(6)
Expected duration: 6 seconds
Putting it to sleep for 2.0 seconds
Waking it up again
Actual duration: 6.07
----------------------------------------------------------------
Starting: import time; time.sleep(6)
Expected duration: 6 seconds
Putting it to sleep for 2.0 seconds
Waking it up again
Actual duration: 8.05
# Suppose:
# - we're blocked until a timeout occurs
# - our process gets put to sleep for a while (SIGSTOP or whatever)
# - then it gets woken up again
# what happens to our timeout?
import os
import signal
import subprocess
import sys
import time
DUR = 6
test_progs = [
f"import select; select.select([], [], [], {DUR})",
f"import select; ep = select.epoll(); ep.poll({DUR})",
f"import threading; ev = threading.Event(); ev.wait({DUR})",
f"import time; time.sleep({DUR})",
]
for test_prog in test_progs:
print("----------------------------------------------------------------")
start = time.monotonic()
print(f"Starting: {test_prog}")
print(f"Expected duration: {DUR} seconds")
p = subprocess.Popen([sys.executable, "-c", test_prog])
time.sleep(DUR / 3)
print(f"Putting it to sleep for {DUR / 3} seconds")
os.kill(p.pid, signal.SIGSTOP)
time.sleep(DUR / 3)
print("Waking it up again")
os.kill(p.pid, signal.SIGCONT)
p.wait()
end = time.monotonic()
print(f"Actual duration: {end - start:.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment