Skip to content

Instantly share code, notes, and snippets.

@Ariakenom
Last active August 28, 2019 13:28
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 Ariakenom/b9275f67c1b3068b247d9aeb6dac795b to your computer and use it in GitHub Desktop.
Save Ariakenom/b9275f67c1b3068b247d9aeb6dac795b to your computer and use it in GitHub Desktop.
def _queue_get(q, timeout):
"""
The timeout of Queue.get has a precision of ~16ms on Windows.
In order to get more precision we poll the last few ms.
"""
t0 = time.perf_counter()
SLEEP_PRECISION = time.get_clock_info('monotonic').resolution
try:
return q.get(timeout=timeout-SLEEP_PRECISION)
except queue.Empty:
pass
# poll
t1 = time.perf_counter()
while 0 < (t0+timeout) - t1:
try:
return q.get(block=False)
except queue.Empty:
t1 = time.perf_counter()
# nothing found raise exception
raise queue.Empty
def _sleep(td):
"""
Default sleep procedure doesn't always sleep the full amount.
This sleeps at least t seconds.
Not sleeping long enough was encountered on windows 10, py3.6
Default sleep has a precision of ~16ms on Windows.
In order to get more precision we busy wait the last few ms.
"""
t0 = time.perf_counter()
SLEEP_PRECISION = time.get_clock_info('monotonic').resolution
time.sleep(max(0, td - SLEEP_PRECISION))
# busy wait
t1 = t0
while 0 < (t0+td) - t1:
t1 = time.perf_counter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment