openmedialibrary_platform/Darwin/lib/python3.5/multiprocessing/queues.py

356 lines
11 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
#
# Module implementing queues
#
# multiprocessing/queues.py
#
# Copyright (c) 2006-2008, R Oudkerk
2014-09-30 16:15:32 +00:00
# Licensed to PSF under a Contributor Agreement.
2013-10-11 17:28:32 +00:00
#
__all__ = ['Queue', 'SimpleQueue', 'JoinableQueue']
import sys
import os
import threading
import collections
import time
import weakref
2014-09-30 16:15:32 +00:00
import errno
from queue import Empty, Full
2013-10-11 17:28:32 +00:00
import _multiprocessing
2014-09-30 16:15:32 +00:00
from . import connection
from . import context
from .util import debug, info, Finalize, register_after_fork, is_exiting
from .reduction import ForkingPickler
2013-10-11 17:28:32 +00:00
#
# Queue type using a pipe, buffer and thread
#
class Queue(object):
2014-09-30 16:15:32 +00:00
def __init__(self, maxsize=0, *, ctx):
2013-10-11 17:28:32 +00:00
if maxsize <= 0:
2015-11-25 01:25:01 +00:00
# Can raise ImportError (see issues #3770 and #23400)
from .synchronize import SEM_VALUE_MAX as maxsize
2013-10-11 17:28:32 +00:00
self._maxsize = maxsize
2014-09-30 16:15:32 +00:00
self._reader, self._writer = connection.Pipe(duplex=False)
self._rlock = ctx.Lock()
2013-10-11 17:28:32 +00:00
self._opid = os.getpid()
if sys.platform == 'win32':
self._wlock = None
else:
2014-09-30 16:15:32 +00:00
self._wlock = ctx.Lock()
self._sem = ctx.BoundedSemaphore(maxsize)
# For use by concurrent.futures
self._ignore_epipe = False
2013-10-11 17:28:32 +00:00
self._after_fork()
if sys.platform != 'win32':
register_after_fork(self, Queue._after_fork)
def __getstate__(self):
2014-09-30 16:15:32 +00:00
context.assert_spawning(self)
return (self._ignore_epipe, self._maxsize, self._reader, self._writer,
2013-10-11 17:28:32 +00:00
self._rlock, self._wlock, self._sem, self._opid)
def __setstate__(self, state):
2014-09-30 16:15:32 +00:00
(self._ignore_epipe, self._maxsize, self._reader, self._writer,
2013-10-11 17:28:32 +00:00
self._rlock, self._wlock, self._sem, self._opid) = state
self._after_fork()
def _after_fork(self):
debug('Queue._after_fork()')
self._notempty = threading.Condition(threading.Lock())
self._buffer = collections.deque()
self._thread = None
self._jointhread = None
self._joincancelled = False
self._closed = False
self._close = None
2014-09-30 16:15:32 +00:00
self._send_bytes = self._writer.send_bytes
self._recv_bytes = self._reader.recv_bytes
2013-10-11 17:28:32 +00:00
self._poll = self._reader.poll
def put(self, obj, block=True, timeout=None):
assert not self._closed
if not self._sem.acquire(block, timeout):
raise Full
2015-11-25 01:25:01 +00:00
with self._notempty:
2013-10-11 17:28:32 +00:00
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._notempty.notify()
def get(self, block=True, timeout=None):
if block and timeout is None:
2014-09-30 16:15:32 +00:00
with self._rlock:
res = self._recv_bytes()
self._sem.release()
2013-10-11 17:28:32 +00:00
else:
if block:
deadline = time.time() + timeout
if not self._rlock.acquire(block, timeout):
raise Empty
try:
if block:
timeout = deadline - time.time()
if timeout < 0 or not self._poll(timeout):
raise Empty
elif not self._poll():
raise Empty
2014-09-30 16:15:32 +00:00
res = self._recv_bytes()
2013-10-11 17:28:32 +00:00
self._sem.release()
finally:
self._rlock.release()
2014-09-30 16:15:32 +00:00
# unserialize the data after having released the lock
return ForkingPickler.loads(res)
2013-10-11 17:28:32 +00:00
def qsize(self):
# Raises NotImplementedError on Mac OSX because of broken sem_getvalue()
return self._maxsize - self._sem._semlock._get_value()
def empty(self):
return not self._poll()
def full(self):
return self._sem._semlock._is_zero()
def get_nowait(self):
return self.get(False)
def put_nowait(self, obj):
return self.put(obj, False)
def close(self):
self._closed = True
2015-11-25 01:25:01 +00:00
try:
self._reader.close()
finally:
close = self._close
if close:
self._close = None
close()
2013-10-11 17:28:32 +00:00
def join_thread(self):
debug('Queue.join_thread()')
assert self._closed
if self._jointhread:
self._jointhread()
def cancel_join_thread(self):
debug('Queue.cancel_join_thread()')
self._joincancelled = True
try:
self._jointhread.cancel()
except AttributeError:
pass
def _start_thread(self):
debug('Queue._start_thread()')
# Start thread which transfers data from buffer to pipe
self._buffer.clear()
self._thread = threading.Thread(
target=Queue._feed,
2014-09-30 16:15:32 +00:00
args=(self._buffer, self._notempty, self._send_bytes,
self._wlock, self._writer.close, self._ignore_epipe),
2013-10-11 17:28:32 +00:00
name='QueueFeederThread'
)
self._thread.daemon = True
debug('doing self._thread.start()')
self._thread.start()
debug('... done self._thread.start()')
# On process exit we will wait for data to be flushed to pipe.
2014-09-30 16:15:32 +00:00
#
# However, if this process created the queue then all
# processes which use the queue will be descendants of this
# process. Therefore waiting for the queue to be flushed
# is pointless once all the child processes have been joined.
created_by_this_process = (self._opid == os.getpid())
if not self._joincancelled and not created_by_this_process:
2013-10-11 17:28:32 +00:00
self._jointhread = Finalize(
self._thread, Queue._finalize_join,
[weakref.ref(self._thread)],
exitpriority=-5
)
# Send sentinel to the thread queue object when garbage collected
self._close = Finalize(
self, Queue._finalize_close,
[self._buffer, self._notempty],
exitpriority=10
)
@staticmethod
def _finalize_join(twr):
debug('joining queue thread')
thread = twr()
if thread is not None:
thread.join()
debug('... queue thread joined')
else:
debug('... queue thread already dead')
@staticmethod
def _finalize_close(buffer, notempty):
debug('telling queue thread to quit')
2015-11-25 01:25:01 +00:00
with notempty:
2013-10-11 17:28:32 +00:00
buffer.append(_sentinel)
notempty.notify()
@staticmethod
2014-09-30 16:15:32 +00:00
def _feed(buffer, notempty, send_bytes, writelock, close, ignore_epipe):
2013-10-11 17:28:32 +00:00
debug('starting thread to feed data to pipe')
nacquire = notempty.acquire
nrelease = notempty.release
nwait = notempty.wait
bpopleft = buffer.popleft
sentinel = _sentinel
if sys.platform != 'win32':
wacquire = writelock.acquire
wrelease = writelock.release
else:
wacquire = None
try:
while 1:
nacquire()
try:
if not buffer:
nwait()
finally:
nrelease()
try:
while 1:
obj = bpopleft()
if obj is sentinel:
debug('feeder thread got sentinel -- exiting')
close()
return
2014-09-30 16:15:32 +00:00
# serialize the data before acquiring the lock
obj = ForkingPickler.dumps(obj)
2013-10-11 17:28:32 +00:00
if wacquire is None:
2014-09-30 16:15:32 +00:00
send_bytes(obj)
2013-10-11 17:28:32 +00:00
else:
wacquire()
try:
2014-09-30 16:15:32 +00:00
send_bytes(obj)
2013-10-11 17:28:32 +00:00
finally:
wrelease()
except IndexError:
pass
2014-09-30 16:15:32 +00:00
except Exception as e:
if ignore_epipe and getattr(e, 'errno', 0) == errno.EPIPE:
return
2013-10-11 17:28:32 +00:00
# Since this runs in a daemon thread the resources it uses
# may be become unusable while the process is cleaning up.
# We ignore errors which happen after the process has
# started to cleanup.
try:
if is_exiting():
info('error in queue thread: %s', e)
else:
import traceback
traceback.print_exc()
except Exception:
pass
_sentinel = object()
#
# A queue type which also supports join() and task_done() methods
#
# Note that if you do not call task_done() for each finished task then
# eventually the counter's semaphore may overflow causing Bad Things
# to happen.
#
class JoinableQueue(Queue):
2014-09-30 16:15:32 +00:00
def __init__(self, maxsize=0, *, ctx):
Queue.__init__(self, maxsize, ctx=ctx)
self._unfinished_tasks = ctx.Semaphore(0)
self._cond = ctx.Condition()
2013-10-11 17:28:32 +00:00
def __getstate__(self):
return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks)
def __setstate__(self, state):
Queue.__setstate__(self, state[:-2])
self._cond, self._unfinished_tasks = state[-2:]
def put(self, obj, block=True, timeout=None):
assert not self._closed
if not self._sem.acquire(block, timeout):
raise Full
2015-11-25 01:25:01 +00:00
with self._notempty, self._cond:
2013-10-11 17:28:32 +00:00
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._unfinished_tasks.release()
self._notempty.notify()
def task_done(self):
2015-11-25 01:25:01 +00:00
with self._cond:
2013-10-11 17:28:32 +00:00
if not self._unfinished_tasks.acquire(False):
raise ValueError('task_done() called too many times')
if self._unfinished_tasks._semlock._is_zero():
self._cond.notify_all()
def join(self):
2015-11-25 01:25:01 +00:00
with self._cond:
2013-10-11 17:28:32 +00:00
if not self._unfinished_tasks._semlock._is_zero():
self._cond.wait()
#
# Simplified Queue type -- really just a locked pipe
#
class SimpleQueue(object):
2014-09-30 16:15:32 +00:00
def __init__(self, *, ctx):
self._reader, self._writer = connection.Pipe(duplex=False)
self._rlock = ctx.Lock()
self._poll = self._reader.poll
2013-10-11 17:28:32 +00:00
if sys.platform == 'win32':
self._wlock = None
else:
2014-09-30 16:15:32 +00:00
self._wlock = ctx.Lock()
2013-10-11 17:28:32 +00:00
def empty(self):
2014-09-30 16:15:32 +00:00
return not self._poll()
2013-10-11 17:28:32 +00:00
def __getstate__(self):
2014-09-30 16:15:32 +00:00
context.assert_spawning(self)
2013-10-11 17:28:32 +00:00
return (self._reader, self._writer, self._rlock, self._wlock)
def __setstate__(self, state):
(self._reader, self._writer, self._rlock, self._wlock) = state
2014-09-30 16:15:32 +00:00
def get(self):
with self._rlock:
res = self._reader.recv_bytes()
# unserialize the data after having released the lock
return ForkingPickler.loads(res)
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def put(self, obj):
# serialize the data before acquiring the lock
obj = ForkingPickler.dumps(obj)
2013-10-11 17:28:32 +00:00
if self._wlock is None:
# writes to a message oriented win32 pipe are atomic
2014-09-30 16:15:32 +00:00
self._writer.send_bytes(obj)
2013-10-11 17:28:32 +00:00
else:
2014-09-30 16:15:32 +00:00
with self._wlock:
self._writer.send_bytes(obj)