annotate Lib/multiprocessing/queues.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 26b1ad09d1a7
children 65d1682dcd5e ca51996099fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47696
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
1 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
2 # Module implementing queues
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
3 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
4 # multiprocessing/queues.py
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
5 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
6 # Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
7 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
8
52506
26b1ad09d1a7 merge 70792 to 26 maint
Jesse Noller <jnoller@gmail.com>
parents: 48289
diff changeset
9 __all__ = ['Queue', 'SimpleQueue', 'JoinableQueue']
47696
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
10
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
11 import sys
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
12 import os
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
13 import threading
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
14 import collections
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
15 import time
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
16 import atexit
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
17 import weakref
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
18
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
19 from Queue import Empty, Full
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
20 import _multiprocessing
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
21 from multiprocessing import Pipe
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
22 from multiprocessing.synchronize import Lock, BoundedSemaphore, Semaphore, Condition
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
23 from multiprocessing.util import debug, info, Finalize, register_after_fork
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
24 from multiprocessing.forking import assert_spawning
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
25
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
26 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
27 # Queue type using a pipe, buffer and thread
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
28 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
29
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
30 class Queue(object):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
31
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
32 def __init__(self, maxsize=0):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
33 if maxsize <= 0:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
34 maxsize = _multiprocessing.SemLock.SEM_VALUE_MAX
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
35 self._maxsize = maxsize
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
36 self._reader, self._writer = Pipe(duplex=False)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
37 self._rlock = Lock()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
38 self._opid = os.getpid()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
39 if sys.platform == 'win32':
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
40 self._wlock = None
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
41 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
42 self._wlock = Lock()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
43 self._sem = BoundedSemaphore(maxsize)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
44
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
45 self._after_fork()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
46
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
47 if sys.platform != 'win32':
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
48 register_after_fork(self, Queue._after_fork)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
49
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
50 def __getstate__(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
51 assert_spawning(self)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
52 return (self._maxsize, self._reader, self._writer,
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
53 self._rlock, self._wlock, self._sem, self._opid)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
54
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
55 def __setstate__(self, state):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
56 (self._maxsize, self._reader, self._writer,
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
57 self._rlock, self._wlock, self._sem, self._opid) = state
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
58 self._after_fork()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
59
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
60 def _after_fork(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
61 debug('Queue._after_fork()')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
62 self._notempty = threading.Condition(threading.Lock())
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
63 self._buffer = collections.deque()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
64 self._thread = None
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
65 self._jointhread = None
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
66 self._joincancelled = False
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
67 self._closed = False
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
68 self._close = None
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
69 self._send = self._writer.send
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
70 self._recv = self._reader.recv
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
71 self._poll = self._reader.poll
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
72
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
73 def put(self, obj, block=True, timeout=None):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
74 assert not self._closed
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
75 if not self._sem.acquire(block, timeout):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
76 raise Full
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
77
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
78 self._notempty.acquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
79 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
80 if self._thread is None:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
81 self._start_thread()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
82 self._buffer.append(obj)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
83 self._notempty.notify()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
84 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
85 self._notempty.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
86
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
87 def get(self, block=True, timeout=None):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
88 if block and timeout is None:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
89 self._rlock.acquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
90 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
91 res = self._recv()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
92 self._sem.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
93 return res
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
94 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
95 self._rlock.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
96
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
97 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
98 if block:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
99 deadline = time.time() + timeout
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
100 if not self._rlock.acquire(block, timeout):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
101 raise Empty
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
102 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
103 if not self._poll(block and (deadline-time.time()) or 0.0):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
104 raise Empty
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
105 res = self._recv()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
106 self._sem.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
107 return res
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
108 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
109 self._rlock.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
110
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
111 def qsize(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
112 # Raises NotImplementError on Mac OSX because of broken sem_getvalue()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
113 return self._maxsize - self._sem._semlock._get_value()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
114
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
115 def empty(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
116 return not self._poll()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
117
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
118 def full(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
119 return self._sem._semlock._is_zero()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
120
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
121 def get_nowait(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
122 return self.get(False)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
123
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
124 def put_nowait(self, obj):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
125 return self.put(obj, False)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
126
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
127 def close(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
128 self._closed = True
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
129 self._reader.close()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
130 if self._close:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
131 self._close()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
132
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
133 def join_thread(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
134 debug('Queue.join_thread()')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
135 assert self._closed
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
136 if self._jointhread:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
137 self._jointhread()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
138
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
139 def cancel_join_thread(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
140 debug('Queue.cancel_join_thread()')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
141 self._joincancelled = True
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
142 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
143 self._jointhread.cancel()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
144 except AttributeError:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
145 pass
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
146
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
147 def _start_thread(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
148 debug('Queue._start_thread()')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
149
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
150 # Start thread which transfers data from buffer to pipe
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
151 self._buffer.clear()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
152 self._thread = threading.Thread(
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
153 target=Queue._feed,
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
154 args=(self._buffer, self._notempty, self._send,
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
155 self._wlock, self._writer.close),
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
156 name='QueueFeederThread'
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
157 )
48289
024b2b984866 change a few uses of the threading APIs
Benjamin Peterson <benjamin@python.org>
parents: 47696
diff changeset
158 self._thread.daemon = True
47696
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
159
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
160 debug('doing self._thread.start()')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
161 self._thread.start()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
162 debug('... done self._thread.start()')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
163
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
164 # On process exit we will wait for data to be flushed to pipe.
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
165 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
166 # However, if this process created the queue then all
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
167 # processes which use the queue will be descendants of this
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
168 # process. Therefore waiting for the queue to be flushed
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
169 # is pointless once all the child processes have been joined.
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
170 created_by_this_process = (self._opid == os.getpid())
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
171 if not self._joincancelled and not created_by_this_process:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
172 self._jointhread = Finalize(
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
173 self._thread, Queue._finalize_join,
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
174 [weakref.ref(self._thread)],
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
175 exitpriority=-5
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
176 )
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
177
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
178 # Send sentinel to the thread queue object when garbage collected
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
179 self._close = Finalize(
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
180 self, Queue._finalize_close,
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
181 [self._buffer, self._notempty],
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
182 exitpriority=10
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
183 )
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
184
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
185 @staticmethod
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
186 def _finalize_join(twr):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
187 debug('joining queue thread')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
188 thread = twr()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
189 if thread is not None:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
190 thread.join()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
191 debug('... queue thread joined')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
192 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
193 debug('... queue thread already dead')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
194
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
195 @staticmethod
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
196 def _finalize_close(buffer, notempty):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
197 debug('telling queue thread to quit')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
198 notempty.acquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
199 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
200 buffer.append(_sentinel)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
201 notempty.notify()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
202 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
203 notempty.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
204
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
205 @staticmethod
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
206 def _feed(buffer, notempty, send, writelock, close):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
207 debug('starting thread to feed data to pipe')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
208 from .util import is_exiting
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
209
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
210 nacquire = notempty.acquire
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
211 nrelease = notempty.release
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
212 nwait = notempty.wait
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
213 bpopleft = buffer.popleft
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
214 sentinel = _sentinel
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
215 if sys.platform != 'win32':
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
216 wacquire = writelock.acquire
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
217 wrelease = writelock.release
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
218 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
219 wacquire = None
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
220
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
221 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
222 while 1:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
223 nacquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
224 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
225 if not buffer:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
226 nwait()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
227 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
228 nrelease()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
229 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
230 while 1:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
231 obj = bpopleft()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
232 if obj is sentinel:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
233 debug('feeder thread got sentinel -- exiting')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
234 close()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
235 return
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
236
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
237 if wacquire is None:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
238 send(obj)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
239 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
240 wacquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
241 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
242 send(obj)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
243 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
244 wrelease()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
245 except IndexError:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
246 pass
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
247 except Exception, e:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
248 # Since this runs in a daemon thread the resources it uses
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
249 # may be become unusable while the process is cleaning up.
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
250 # We ignore errors which happen after the process has
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
251 # started to cleanup.
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
252 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
253 if is_exiting():
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
254 info('error in queue thread: %s', e)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
255 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
256 import traceback
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
257 traceback.print_exc()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
258 except Exception:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
259 pass
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
260
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
261 _sentinel = object()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
262
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
263 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
264 # A queue type which also supports join() and task_done() methods
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
265 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
266 # Note that if you do not call task_done() for each finished task then
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
267 # eventually the counter's semaphore may overflow causing Bad Things
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
268 # to happen.
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
269 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
270
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
271 class JoinableQueue(Queue):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
272
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
273 def __init__(self, maxsize=0):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
274 Queue.__init__(self, maxsize)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
275 self._unfinished_tasks = Semaphore(0)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
276 self._cond = Condition()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
277
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
278 def __getstate__(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
279 return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
280
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
281 def __setstate__(self, state):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
282 Queue.__setstate__(self, state[:-2])
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
283 self._cond, self._unfinished_tasks = state[-2:]
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
284
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
285 def put(self, item, block=True, timeout=None):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
286 Queue.put(self, item, block, timeout)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
287 self._unfinished_tasks.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
288
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
289 def task_done(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
290 self._cond.acquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
291 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
292 if not self._unfinished_tasks.acquire(False):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
293 raise ValueError('task_done() called too many times')
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
294 if self._unfinished_tasks._semlock._is_zero():
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
295 self._cond.notify_all()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
296 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
297 self._cond.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
298
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
299 def join(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
300 self._cond.acquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
301 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
302 if not self._unfinished_tasks._semlock._is_zero():
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
303 self._cond.wait()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
304 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
305 self._cond.release()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
306
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
307 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
308 # Simplified Queue type -- really just a locked pipe
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
309 #
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
310
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
311 class SimpleQueue(object):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
312
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
313 def __init__(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
314 self._reader, self._writer = Pipe(duplex=False)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
315 self._rlock = Lock()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
316 if sys.platform == 'win32':
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
317 self._wlock = None
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
318 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
319 self._wlock = Lock()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
320 self._make_methods()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
321
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
322 def empty(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
323 return not self._reader.poll()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
324
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
325 def __getstate__(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
326 assert_spawning(self)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
327 return (self._reader, self._writer, self._rlock, self._wlock)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
328
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
329 def __setstate__(self, state):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
330 (self._reader, self._writer, self._rlock, self._wlock) = state
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
331 self._make_methods()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
332
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
333 def _make_methods(self):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
334 recv = self._reader.recv
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
335 racquire, rrelease = self._rlock.acquire, self._rlock.release
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
336 def get():
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
337 racquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
338 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
339 return recv()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
340 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
341 rrelease()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
342 self.get = get
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
343
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
344 if self._wlock is None:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
345 # writes to a message oriented win32 pipe are atomic
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
346 self.put = self._writer.send
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
347 else:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
348 send = self._writer.send
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
349 wacquire, wrelease = self._wlock.acquire, self._wlock.release
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
350 def put(obj):
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
351 wacquire()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
352 try:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
353 return send(obj)
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
354 finally:
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
355 wrelease()
d01165fae98e darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
356 self.put = put