Skip to content

Commit

Permalink
bpo-34054: multiprocessing uses time.monotonic() (pythonGH-8118)
Browse files Browse the repository at this point in the history
The multiprocessing module now uses the monotonic clock
time.monotonic() instead of the system clock time.time() to implement
timeouts.
(cherry picked from commit c2368cb)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
  • Loading branch information
miss-islington and vstinner authored Jul 6, 2018
1 parent e544d65 commit 972458a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Lib/multiprocessing/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@


def _init_timeout(timeout=CONNECTION_TIMEOUT):
return time.time() + timeout
return time.monotonic() + timeout

def _check_timeout(t):
return time.time() > t
return time.monotonic() > t

#
#
Expand Down Expand Up @@ -905,15 +905,15 @@ def wait(object_list, timeout=None):
selector.register(obj, selectors.EVENT_READ)

if timeout is not None:
deadline = time.time() + timeout
deadline = time.monotonic() + timeout

while True:
ready = selector.select(timeout)
if ready:
return [key.fileobj for (key, events) in ready]
else:
if timeout is not None:
timeout = deadline - time.time()
timeout = deadline - time.monotonic()
if timeout < 0:
return ready

Expand Down
6 changes: 3 additions & 3 deletions Lib/multiprocessing/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import threading
import array
import queue
import time

from time import time as _time
from traceback import format_exc

from . import connection
Expand Down Expand Up @@ -1006,13 +1006,13 @@ def wait_for(self, predicate, timeout=None):
if result:
return result
if timeout is not None:
endtime = _time() + timeout
endtime = time.monotonic() + timeout
else:
endtime = None
waittime = None
while not result:
if endtime is not None:
waittime = endtime - _time()
waittime = endtime - time.monotonic()
if waittime <= 0:
break
self.wait(waittime)
Expand Down
4 changes: 2 additions & 2 deletions Lib/multiprocessing/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ def get(self, block=True, timeout=None):
self._sem.release()
else:
if block:
deadline = time.time() + timeout
deadline = time.monotonic() + timeout
if not self._rlock.acquire(block, timeout):
raise Empty
try:
if block:
timeout = deadline - time.time()
timeout = deadline - time.monotonic()
if not self._poll(timeout):
raise Empty
elif not self._poll():
Expand Down
7 changes: 3 additions & 4 deletions Lib/multiprocessing/synchronize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import sys
import tempfile
import _multiprocessing

from time import time as _time
import time

from . import context
from . import process
Expand Down Expand Up @@ -313,13 +312,13 @@ def wait_for(self, predicate, timeout=None):
if result:
return result
if timeout is not None:
endtime = _time() + timeout
endtime = time.monotonic() + timeout
else:
endtime = None
waittime = None
while not result:
if endtime is not None:
waittime = endtime - _time()
waittime = endtime - time.monotonic()
if waittime <= 0:
break
self.wait(waittime)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The multiprocessing module now uses the monotonic clock
:func:`time.monotonic` instead of the system clock :func:`time.time` to
implement timeout.

0 comments on commit 972458a

Please sign in to comment.