mirror of
https://github.com/python/cpython.git
synced 2024-11-26 03:14:27 +08:00
8719ad5dde
svn+ssh://pythondev@svn.python.org/python/trunk ........ r74277 | sean.reifschneider | 2009-08-01 18:54:55 -0500 (Sat, 01 Aug 2009) | 3 lines - Issue #6624: yArg_ParseTuple with "s" format when parsing argument with NUL: Bogus TypeError detail string. ........ r74321 | guilherme.polo | 2009-08-05 11:51:41 -0500 (Wed, 05 Aug 2009) | 1 line Easier reference to find (at least while svn continues being used). ........ r74323 | guilherme.polo | 2009-08-05 18:48:26 -0500 (Wed, 05 Aug 2009) | 1 line Typo. ........ r74326 | jesse.noller | 2009-08-05 21:05:56 -0500 (Wed, 05 Aug 2009) | 1 line Fix issue 4660: spurious task_done errors in multiprocessing, remove doc note for from_address ........ r74355 | gregory.p.smith | 2009-08-12 12:02:37 -0500 (Wed, 12 Aug 2009) | 2 lines comment typo fix ........ r74465 | vinay.sajip | 2009-08-15 18:23:12 -0500 (Sat, 15 Aug 2009) | 1 line Added section on logging to one file from multiple processes. ........ r74467 | vinay.sajip | 2009-08-15 18:34:47 -0500 (Sat, 15 Aug 2009) | 1 line Refined section on logging to one file from multiple processes. ........ r74488 | vinay.sajip | 2009-08-17 08:14:37 -0500 (Mon, 17 Aug 2009) | 1 line Further refined section on logging to one file from multiple processes. ........ r74492 | r.david.murray | 2009-08-17 14:26:49 -0500 (Mon, 17 Aug 2009) | 2 lines Issue 6685: 'toupper' -> 'upper' in cgi doc example explanation. ........ r74513 | skip.montanaro | 2009-08-18 09:37:52 -0500 (Tue, 18 Aug 2009) | 1 line missing module ref (issue6723) ........ r74531 | vinay.sajip | 2009-08-20 17:04:32 -0500 (Thu, 20 Aug 2009) | 1 line Added section on exceptions raised during logging. ........ r74549 | benjamin.peterson | 2009-08-24 12:42:36 -0500 (Mon, 24 Aug 2009) | 1 line fix pdf building by teaching latex the right encoding package ........ r74553 | r.david.murray | 2009-08-26 20:04:59 -0500 (Wed, 26 Aug 2009) | 2 lines Remove leftover text from end of sentence. ........ r74625 | benjamin.peterson | 2009-09-01 17:27:57 -0500 (Tue, 01 Sep 2009) | 1 line remove the check that classmethod's argument is a callable ........ r74632 | georg.brandl | 2009-09-03 02:27:26 -0500 (Thu, 03 Sep 2009) | 1 line #6828: fix wrongly highlighted blocks. ........ r74643 | georg.brandl | 2009-09-04 01:59:20 -0500 (Fri, 04 Sep 2009) | 2 lines Issue #2666: Handle BROWSER environment variable properly for unknown browser names in the webbrowser module. ........ r74644 | georg.brandl | 2009-09-04 02:55:14 -0500 (Fri, 04 Sep 2009) | 1 line #5047: remove Monterey support from configure. ........ r74647 | georg.brandl | 2009-09-04 03:17:04 -0500 (Fri, 04 Sep 2009) | 2 lines Issue #5275: In Cookie's Cookie.load(), properly handle non-string arguments as documented. ........ r74652 | georg.brandl | 2009-09-04 06:25:37 -0500 (Fri, 04 Sep 2009) | 1 line #6756: add some info about the "acct" parameter. ........ r74666 | georg.brandl | 2009-09-05 04:04:09 -0500 (Sat, 05 Sep 2009) | 1 line #6841: remove duplicated word. ........ r74671 | georg.brandl | 2009-09-05 11:47:17 -0500 (Sat, 05 Sep 2009) | 1 line #6843: add link from filterwarnings to where the meaning of the arguments is covered. ........ r74727 | benjamin.peterson | 2009-09-08 18:04:22 -0500 (Tue, 08 Sep 2009) | 1 line #6865 fix ref counting in initialization of pwd module ........ r74739 | georg.brandl | 2009-09-11 02:55:20 -0500 (Fri, 11 Sep 2009) | 1 line Move function back to its section. ........
370 lines
11 KiB
Python
370 lines
11 KiB
Python
#
|
|
# Module implementing queues
|
|
#
|
|
# multiprocessing/queues.py
|
|
#
|
|
# Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
|
|
#
|
|
|
|
__all__ = ['Queue', 'SimpleQueue', 'JoinableQueue']
|
|
|
|
import sys
|
|
import os
|
|
import threading
|
|
import collections
|
|
import time
|
|
import atexit
|
|
import weakref
|
|
|
|
from queue import Empty, Full
|
|
import _multiprocessing
|
|
from multiprocessing import Pipe
|
|
from multiprocessing.synchronize import Lock, BoundedSemaphore, Semaphore, Condition
|
|
from multiprocessing.util import debug, info, Finalize, register_after_fork
|
|
from multiprocessing.forking import assert_spawning
|
|
|
|
#
|
|
# Queue type using a pipe, buffer and thread
|
|
#
|
|
|
|
class Queue(object):
|
|
|
|
def __init__(self, maxsize=0):
|
|
if maxsize <= 0:
|
|
maxsize = _multiprocessing.SemLock.SEM_VALUE_MAX
|
|
self._maxsize = maxsize
|
|
self._reader, self._writer = Pipe(duplex=False)
|
|
self._rlock = Lock()
|
|
self._opid = os.getpid()
|
|
if sys.platform == 'win32':
|
|
self._wlock = None
|
|
else:
|
|
self._wlock = Lock()
|
|
self._sem = BoundedSemaphore(maxsize)
|
|
|
|
self._after_fork()
|
|
|
|
if sys.platform != 'win32':
|
|
register_after_fork(self, Queue._after_fork)
|
|
|
|
def __getstate__(self):
|
|
assert_spawning(self)
|
|
return (self._maxsize, self._reader, self._writer,
|
|
self._rlock, self._wlock, self._sem, self._opid)
|
|
|
|
def __setstate__(self, state):
|
|
(self._maxsize, self._reader, self._writer,
|
|
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
|
|
self._send = self._writer.send
|
|
self._recv = self._reader.recv
|
|
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
|
|
|
|
self._notempty.acquire()
|
|
try:
|
|
if self._thread is None:
|
|
self._start_thread()
|
|
self._buffer.append(obj)
|
|
self._notempty.notify()
|
|
finally:
|
|
self._notempty.release()
|
|
|
|
def get(self, block=True, timeout=None):
|
|
if block and timeout is None:
|
|
self._rlock.acquire()
|
|
try:
|
|
res = self._recv()
|
|
self._sem.release()
|
|
return res
|
|
finally:
|
|
self._rlock.release()
|
|
|
|
else:
|
|
if block:
|
|
deadline = time.time() + timeout
|
|
if not self._rlock.acquire(block, timeout):
|
|
raise Empty
|
|
try:
|
|
if not self._poll(block and (deadline-time.time()) or 0.0):
|
|
raise Empty
|
|
res = self._recv()
|
|
self._sem.release()
|
|
return res
|
|
finally:
|
|
self._rlock.release()
|
|
|
|
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
|
|
self._reader.close()
|
|
if self._close:
|
|
self._close()
|
|
|
|
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,
|
|
args=(self._buffer, self._notempty, self._send,
|
|
self._wlock, self._writer.close),
|
|
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.
|
|
#
|
|
# 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:
|
|
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')
|
|
notempty.acquire()
|
|
try:
|
|
buffer.append(_sentinel)
|
|
notempty.notify()
|
|
finally:
|
|
notempty.release()
|
|
|
|
@staticmethod
|
|
def _feed(buffer, notempty, send, writelock, close):
|
|
debug('starting thread to feed data to pipe')
|
|
from .util import is_exiting
|
|
|
|
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
|
|
|
|
if wacquire is None:
|
|
send(obj)
|
|
else:
|
|
wacquire()
|
|
try:
|
|
send(obj)
|
|
finally:
|
|
wrelease()
|
|
except IndexError:
|
|
pass
|
|
except Exception as e:
|
|
# 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):
|
|
|
|
def __init__(self, maxsize=0):
|
|
Queue.__init__(self, maxsize)
|
|
self._unfinished_tasks = Semaphore(0)
|
|
self._cond = Condition()
|
|
|
|
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
|
|
|
|
self._notempty.acquire()
|
|
self._cond.acquire()
|
|
try:
|
|
if self._thread is None:
|
|
self._start_thread()
|
|
self._buffer.append(obj)
|
|
self._unfinished_tasks.release()
|
|
self._notempty.notify()
|
|
finally:
|
|
self._cond.release()
|
|
self._notempty.release()
|
|
|
|
def task_done(self):
|
|
self._cond.acquire()
|
|
try:
|
|
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()
|
|
finally:
|
|
self._cond.release()
|
|
|
|
def join(self):
|
|
self._cond.acquire()
|
|
try:
|
|
if not self._unfinished_tasks._semlock._is_zero():
|
|
self._cond.wait()
|
|
finally:
|
|
self._cond.release()
|
|
|
|
#
|
|
# Simplified Queue type -- really just a locked pipe
|
|
#
|
|
|
|
class SimpleQueue(object):
|
|
|
|
def __init__(self):
|
|
self._reader, self._writer = Pipe(duplex=False)
|
|
self._rlock = Lock()
|
|
if sys.platform == 'win32':
|
|
self._wlock = None
|
|
else:
|
|
self._wlock = Lock()
|
|
self._make_methods()
|
|
|
|
def empty(self):
|
|
return not self._reader.poll()
|
|
|
|
def __getstate__(self):
|
|
assert_spawning(self)
|
|
return (self._reader, self._writer, self._rlock, self._wlock)
|
|
|
|
def __setstate__(self, state):
|
|
(self._reader, self._writer, self._rlock, self._wlock) = state
|
|
self._make_methods()
|
|
|
|
def _make_methods(self):
|
|
recv = self._reader.recv
|
|
racquire, rrelease = self._rlock.acquire, self._rlock.release
|
|
def get():
|
|
racquire()
|
|
try:
|
|
return recv()
|
|
finally:
|
|
rrelease()
|
|
self.get = get
|
|
|
|
if self._wlock is None:
|
|
# writes to a message oriented win32 pipe are atomic
|
|
self.put = self._writer.send
|
|
else:
|
|
send = self._writer.send
|
|
wacquire, wrelease = self._wlock.acquire, self._wlock.release
|
|
def put(obj):
|
|
wacquire()
|
|
try:
|
|
return send(obj)
|
|
finally:
|
|
wrelease()
|
|
self.put = put
|