Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 1 | # |
| 2 | # Module implementing queues |
| 3 | # |
| 4 | # multiprocessing/queues.py |
| 5 | # |
R. David Murray | 3fc969a | 2010-12-14 01:38:16 +0000 | [diff] [blame] | 6 | # Copyright (c) 2006-2008, R Oudkerk |
Richard Oudkerk | 3e268aa | 2012-04-30 12:13:55 +0100 | [diff] [blame] | 7 | # Licensed to PSF under a Contributor Agreement. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 8 | # |
| 9 | |
Jesse Noller | 14f3ae2 | 2009-03-31 03:37:07 +0000 | [diff] [blame] | 10 | __all__ = ['Queue', 'SimpleQueue', 'JoinableQueue'] |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 11 | |
| 12 | import sys |
| 13 | import os |
| 14 | import threading |
| 15 | import collections |
| 16 | import time |
Batuhan Taşkaya | 0361556 | 2020-04-10 17:46:36 +0300 | [diff] [blame] | 17 | import types |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 18 | import weakref |
Antoine Pitrou | dc19c24 | 2011-07-16 01:51:58 +0200 | [diff] [blame] | 19 | import errno |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 20 | |
| 21 | from queue import Empty, Full |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 22 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 23 | import _multiprocessing |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 24 | |
| 25 | from . import connection |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 26 | from . import context |
Davin Potts | 5458647 | 2016-09-09 18:03:10 -0500 | [diff] [blame] | 27 | _ForkingPickler = context.reduction.ForkingPickler |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 28 | |
| 29 | from .util import debug, info, Finalize, register_after_fork, is_exiting |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 30 | |
| 31 | # |
| 32 | # Queue type using a pipe, buffer and thread |
| 33 | # |
| 34 | |
| 35 | class Queue(object): |
| 36 | |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 37 | def __init__(self, maxsize=0, *, ctx): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 38 | if maxsize <= 0: |
Berker Peksag | 7ecfc82 | 2015-04-08 17:56:30 +0300 | [diff] [blame] | 39 | # Can raise ImportError (see issues #3770 and #23400) |
| 40 | from .synchronize import SEM_VALUE_MAX as maxsize |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 41 | self._maxsize = maxsize |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 42 | self._reader, self._writer = connection.Pipe(duplex=False) |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 43 | self._rlock = ctx.Lock() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 44 | self._opid = os.getpid() |
| 45 | if sys.platform == 'win32': |
| 46 | self._wlock = None |
| 47 | else: |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 48 | self._wlock = ctx.Lock() |
| 49 | self._sem = ctx.BoundedSemaphore(maxsize) |
Antoine Pitrou | dc19c24 | 2011-07-16 01:51:58 +0200 | [diff] [blame] | 50 | # For use by concurrent.futures |
| 51 | self._ignore_epipe = False |
Dong-hee Na | a5900ec | 2020-04-15 01:35:36 +0900 | [diff] [blame] | 52 | self._reset() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 53 | |
| 54 | if sys.platform != 'win32': |
| 55 | register_after_fork(self, Queue._after_fork) |
| 56 | |
| 57 | def __getstate__(self): |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 58 | context.assert_spawning(self) |
Antoine Pitrou | fb96089 | 2011-07-20 02:01:39 +0200 | [diff] [blame] | 59 | return (self._ignore_epipe, self._maxsize, self._reader, self._writer, |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 60 | self._rlock, self._wlock, self._sem, self._opid) |
| 61 | |
| 62 | def __setstate__(self, state): |
Antoine Pitrou | fb96089 | 2011-07-20 02:01:39 +0200 | [diff] [blame] | 63 | (self._ignore_epipe, self._maxsize, self._reader, self._writer, |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 64 | self._rlock, self._wlock, self._sem, self._opid) = state |
Dong-hee Na | a5900ec | 2020-04-15 01:35:36 +0900 | [diff] [blame] | 65 | self._reset() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 66 | |
| 67 | def _after_fork(self): |
| 68 | debug('Queue._after_fork()') |
Dong-hee Na | a5900ec | 2020-04-15 01:35:36 +0900 | [diff] [blame] | 69 | self._reset(after_fork=True) |
| 70 | |
| 71 | def _reset(self, after_fork=False): |
| 72 | if after_fork: |
| 73 | self._notempty._at_fork_reinit() |
| 74 | else: |
| 75 | self._notempty = threading.Condition(threading.Lock()) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 76 | self._buffer = collections.deque() |
| 77 | self._thread = None |
| 78 | self._jointhread = None |
| 79 | self._joincancelled = False |
| 80 | self._closed = False |
| 81 | self._close = None |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 82 | self._send_bytes = self._writer.send_bytes |
| 83 | self._recv_bytes = self._reader.recv_bytes |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 84 | self._poll = self._reader.poll |
| 85 | |
| 86 | def put(self, obj, block=True, timeout=None): |
Zackery Spytz | 0461704 | 2018-10-13 03:26:09 -0600 | [diff] [blame] | 87 | if self._closed: |
| 88 | raise ValueError(f"Queue {self!r} is closed") |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 89 | if not self._sem.acquire(block, timeout): |
| 90 | raise Full |
| 91 | |
Charles-François Natali | a924fc7 | 2014-05-25 14:12:12 +0100 | [diff] [blame] | 92 | with self._notempty: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 93 | if self._thread is None: |
| 94 | self._start_thread() |
| 95 | self._buffer.append(obj) |
| 96 | self._notempty.notify() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 97 | |
| 98 | def get(self, block=True, timeout=None): |
Zackery Spytz | 0461704 | 2018-10-13 03:26:09 -0600 | [diff] [blame] | 99 | if self._closed: |
| 100 | raise ValueError(f"Queue {self!r} is closed") |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 101 | if block and timeout is None: |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 102 | with self._rlock: |
| 103 | res = self._recv_bytes() |
| 104 | self._sem.release() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 105 | else: |
| 106 | if block: |
Victor Stinner | c2368cb | 2018-07-06 13:51:52 +0200 | [diff] [blame] | 107 | deadline = time.monotonic() + timeout |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 108 | if not self._rlock.acquire(block, timeout): |
| 109 | raise Empty |
| 110 | try: |
Antoine Pitrou | a365113 | 2011-11-10 00:37:09 +0100 | [diff] [blame] | 111 | if block: |
Victor Stinner | c2368cb | 2018-07-06 13:51:52 +0200 | [diff] [blame] | 112 | timeout = deadline - time.monotonic() |
Victor Stinner | 1b7863c | 2017-06-10 13:10:13 +0200 | [diff] [blame] | 113 | if not self._poll(timeout): |
Antoine Pitrou | a365113 | 2011-11-10 00:37:09 +0100 | [diff] [blame] | 114 | raise Empty |
| 115 | elif not self._poll(): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 116 | raise Empty |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 117 | res = self._recv_bytes() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 118 | self._sem.release() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 119 | finally: |
| 120 | self._rlock.release() |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 121 | # unserialize the data after having released the lock |
Davin Potts | 5458647 | 2016-09-09 18:03:10 -0500 | [diff] [blame] | 122 | return _ForkingPickler.loads(res) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 123 | |
| 124 | def qsize(self): |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 125 | # Raises NotImplementedError on Mac OSX because of broken sem_getvalue() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 126 | return self._maxsize - self._sem._semlock._get_value() |
| 127 | |
| 128 | def empty(self): |
| 129 | return not self._poll() |
| 130 | |
| 131 | def full(self): |
| 132 | return self._sem._semlock._is_zero() |
| 133 | |
| 134 | def get_nowait(self): |
| 135 | return self.get(False) |
| 136 | |
| 137 | def put_nowait(self, obj): |
| 138 | return self.put(obj, False) |
| 139 | |
| 140 | def close(self): |
| 141 | self._closed = True |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 142 | try: |
| 143 | self._reader.close() |
| 144 | finally: |
| 145 | close = self._close |
| 146 | if close: |
| 147 | self._close = None |
| 148 | close() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 149 | |
| 150 | def join_thread(self): |
| 151 | debug('Queue.join_thread()') |
Allen W. Smith, Ph.D | bd73e72 | 2017-08-29 17:52:18 -0500 | [diff] [blame] | 152 | assert self._closed, "Queue {0!r} not closed".format(self) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 153 | if self._jointhread: |
| 154 | self._jointhread() |
| 155 | |
| 156 | def cancel_join_thread(self): |
| 157 | debug('Queue.cancel_join_thread()') |
| 158 | self._joincancelled = True |
| 159 | try: |
| 160 | self._jointhread.cancel() |
| 161 | except AttributeError: |
| 162 | pass |
| 163 | |
| 164 | def _start_thread(self): |
| 165 | debug('Queue._start_thread()') |
| 166 | |
| 167 | # Start thread which transfers data from buffer to pipe |
| 168 | self._buffer.clear() |
| 169 | self._thread = threading.Thread( |
| 170 | target=Queue._feed, |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 171 | args=(self._buffer, self._notempty, self._send_bytes, |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 172 | self._wlock, self._writer.close, self._ignore_epipe, |
Thomas Moreau | e2f33ad | 2018-03-21 16:50:28 +0100 | [diff] [blame] | 173 | self._on_queue_feeder_error, self._sem), |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 174 | name='QueueFeederThread' |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 175 | ) |
Benjamin Peterson | 7275370 | 2008-08-18 18:09:21 +0000 | [diff] [blame] | 176 | self._thread.daemon = True |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 177 | |
| 178 | debug('doing self._thread.start()') |
| 179 | self._thread.start() |
| 180 | debug('... done self._thread.start()') |
| 181 | |
Victor Stinner | 3b69d91 | 2017-07-10 12:45:21 +0200 | [diff] [blame] | 182 | if not self._joincancelled: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 183 | self._jointhread = Finalize( |
| 184 | self._thread, Queue._finalize_join, |
| 185 | [weakref.ref(self._thread)], |
| 186 | exitpriority=-5 |
| 187 | ) |
| 188 | |
| 189 | # Send sentinel to the thread queue object when garbage collected |
| 190 | self._close = Finalize( |
| 191 | self, Queue._finalize_close, |
| 192 | [self._buffer, self._notempty], |
| 193 | exitpriority=10 |
| 194 | ) |
| 195 | |
| 196 | @staticmethod |
| 197 | def _finalize_join(twr): |
| 198 | debug('joining queue thread') |
| 199 | thread = twr() |
| 200 | if thread is not None: |
| 201 | thread.join() |
| 202 | debug('... queue thread joined') |
| 203 | else: |
| 204 | debug('... queue thread already dead') |
| 205 | |
| 206 | @staticmethod |
| 207 | def _finalize_close(buffer, notempty): |
| 208 | debug('telling queue thread to quit') |
Charles-François Natali | a924fc7 | 2014-05-25 14:12:12 +0100 | [diff] [blame] | 209 | with notempty: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 210 | buffer.append(_sentinel) |
| 211 | notempty.notify() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 212 | |
| 213 | @staticmethod |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 214 | def _feed(buffer, notempty, send_bytes, writelock, close, ignore_epipe, |
Thomas Moreau | e2f33ad | 2018-03-21 16:50:28 +0100 | [diff] [blame] | 215 | onerror, queue_sem): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 216 | debug('starting thread to feed data to pipe') |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 217 | nacquire = notempty.acquire |
| 218 | nrelease = notempty.release |
| 219 | nwait = notempty.wait |
| 220 | bpopleft = buffer.popleft |
| 221 | sentinel = _sentinel |
| 222 | if sys.platform != 'win32': |
| 223 | wacquire = writelock.acquire |
| 224 | wrelease = writelock.release |
| 225 | else: |
| 226 | wacquire = None |
| 227 | |
grzgrzgrz3 | bc50f03 | 2017-05-25 16:22:57 +0200 | [diff] [blame] | 228 | while 1: |
| 229 | try: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 230 | nacquire() |
| 231 | try: |
| 232 | if not buffer: |
| 233 | nwait() |
| 234 | finally: |
| 235 | nrelease() |
| 236 | try: |
| 237 | while 1: |
| 238 | obj = bpopleft() |
| 239 | if obj is sentinel: |
| 240 | debug('feeder thread got sentinel -- exiting') |
| 241 | close() |
| 242 | return |
| 243 | |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 244 | # serialize the data before acquiring the lock |
Davin Potts | 5458647 | 2016-09-09 18:03:10 -0500 | [diff] [blame] | 245 | obj = _ForkingPickler.dumps(obj) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 246 | if wacquire is None: |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 247 | send_bytes(obj) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 248 | else: |
| 249 | wacquire() |
| 250 | try: |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 251 | send_bytes(obj) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 252 | finally: |
| 253 | wrelease() |
| 254 | except IndexError: |
| 255 | pass |
grzgrzgrz3 | bc50f03 | 2017-05-25 16:22:57 +0200 | [diff] [blame] | 256 | except Exception as e: |
| 257 | if ignore_epipe and getattr(e, 'errno', 0) == errno.EPIPE: |
| 258 | return |
| 259 | # Since this runs in a daemon thread the resources it uses |
| 260 | # may be become unusable while the process is cleaning up. |
| 261 | # We ignore errors which happen after the process has |
| 262 | # started to cleanup. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 263 | if is_exiting(): |
| 264 | info('error in queue thread: %s', e) |
grzgrzgrz3 | bc50f03 | 2017-05-25 16:22:57 +0200 | [diff] [blame] | 265 | return |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 266 | else: |
Thomas Moreau | e2f33ad | 2018-03-21 16:50:28 +0100 | [diff] [blame] | 267 | # Since the object has not been sent in the queue, we need |
| 268 | # to decrease the size of the queue. The error acts as |
| 269 | # if the object had been silently removed from the queue |
| 270 | # and this step is necessary to have a properly working |
| 271 | # queue. |
| 272 | queue_sem.release() |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 273 | onerror(e, obj) |
| 274 | |
| 275 | @staticmethod |
| 276 | def _on_queue_feeder_error(e, obj): |
| 277 | """ |
| 278 | Private API hook called when feeding data in the background thread |
| 279 | raises an exception. For overriding by concurrent.futures. |
| 280 | """ |
| 281 | import traceback |
| 282 | traceback.print_exc() |
| 283 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 284 | |
| 285 | _sentinel = object() |
| 286 | |
| 287 | # |
| 288 | # A queue type which also supports join() and task_done() methods |
| 289 | # |
| 290 | # Note that if you do not call task_done() for each finished task then |
| 291 | # eventually the counter's semaphore may overflow causing Bad Things |
| 292 | # to happen. |
| 293 | # |
| 294 | |
| 295 | class JoinableQueue(Queue): |
| 296 | |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 297 | def __init__(self, maxsize=0, *, ctx): |
| 298 | Queue.__init__(self, maxsize, ctx=ctx) |
| 299 | self._unfinished_tasks = ctx.Semaphore(0) |
| 300 | self._cond = ctx.Condition() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 301 | |
| 302 | def __getstate__(self): |
| 303 | return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks) |
| 304 | |
| 305 | def __setstate__(self, state): |
| 306 | Queue.__setstate__(self, state[:-2]) |
| 307 | self._cond, self._unfinished_tasks = state[-2:] |
| 308 | |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 309 | def put(self, obj, block=True, timeout=None): |
Zackery Spytz | 0461704 | 2018-10-13 03:26:09 -0600 | [diff] [blame] | 310 | if self._closed: |
| 311 | raise ValueError(f"Queue {self!r} is closed") |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 312 | if not self._sem.acquire(block, timeout): |
| 313 | raise Full |
| 314 | |
Charles-François Natali | a924fc7 | 2014-05-25 14:12:12 +0100 | [diff] [blame] | 315 | with self._notempty, self._cond: |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 316 | if self._thread is None: |
| 317 | self._start_thread() |
| 318 | self._buffer.append(obj) |
| 319 | self._unfinished_tasks.release() |
| 320 | self._notempty.notify() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 321 | |
| 322 | def task_done(self): |
Charles-François Natali | a924fc7 | 2014-05-25 14:12:12 +0100 | [diff] [blame] | 323 | with self._cond: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 324 | if not self._unfinished_tasks.acquire(False): |
| 325 | raise ValueError('task_done() called too many times') |
| 326 | if self._unfinished_tasks._semlock._is_zero(): |
| 327 | self._cond.notify_all() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 328 | |
| 329 | def join(self): |
Charles-François Natali | a924fc7 | 2014-05-25 14:12:12 +0100 | [diff] [blame] | 330 | with self._cond: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 331 | if not self._unfinished_tasks._semlock._is_zero(): |
| 332 | self._cond.wait() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 333 | |
| 334 | # |
| 335 | # Simplified Queue type -- really just a locked pipe |
| 336 | # |
| 337 | |
| 338 | class SimpleQueue(object): |
| 339 | |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 340 | def __init__(self, *, ctx): |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 341 | self._reader, self._writer = connection.Pipe(duplex=False) |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 342 | self._rlock = ctx.Lock() |
Antoine Pitrou | bdb1cf1 | 2012-03-05 19:28:37 +0100 | [diff] [blame] | 343 | self._poll = self._reader.poll |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 344 | if sys.platform == 'win32': |
| 345 | self._wlock = None |
| 346 | else: |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 347 | self._wlock = ctx.Lock() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 348 | |
Victor Stinner | 9adccc1 | 2020-04-27 18:11:10 +0200 | [diff] [blame] | 349 | def close(self): |
| 350 | self._reader.close() |
| 351 | self._writer.close() |
| 352 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 353 | def empty(self): |
Antoine Pitrou | bdb1cf1 | 2012-03-05 19:28:37 +0100 | [diff] [blame] | 354 | return not self._poll() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 355 | |
| 356 | def __getstate__(self): |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 357 | context.assert_spawning(self) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 358 | return (self._reader, self._writer, self._rlock, self._wlock) |
| 359 | |
| 360 | def __setstate__(self, state): |
| 361 | (self._reader, self._writer, self._rlock, self._wlock) = state |
Xiang Zhang | 6f75bc0 | 2017-05-17 21:04:00 +0800 | [diff] [blame] | 362 | self._poll = self._reader.poll |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 363 | |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 364 | def get(self): |
| 365 | with self._rlock: |
| 366 | res = self._reader.recv_bytes() |
| 367 | # unserialize the data after having released the lock |
Davin Potts | 5458647 | 2016-09-09 18:03:10 -0500 | [diff] [blame] | 368 | return _ForkingPickler.loads(res) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 369 | |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 370 | def put(self, obj): |
| 371 | # serialize the data before acquiring the lock |
Davin Potts | 5458647 | 2016-09-09 18:03:10 -0500 | [diff] [blame] | 372 | obj = _ForkingPickler.dumps(obj) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 373 | if self._wlock is None: |
| 374 | # writes to a message oriented win32 pipe are atomic |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 375 | self._writer.send_bytes(obj) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 376 | else: |
Charles-François Natali | 84e4316 | 2013-03-25 18:20:40 +0100 | [diff] [blame] | 377 | with self._wlock: |
| 378 | self._writer.send_bytes(obj) |
Batuhan Taşkaya | 0361556 | 2020-04-10 17:46:36 +0300 | [diff] [blame] | 379 | |
| 380 | __class_getitem__ = classmethod(types.GenericAlias) |