| 312 | # |
| 313 | |
| 314 | class JoinableQueue(Queue): |
| 315 | |
| 316 | def __init__(self, maxsize=0, *, ctx): |
| 317 | Queue.__init__(self, maxsize, ctx=ctx) |
| 318 | self._unfinished_tasks = ctx.Semaphore(0) |
| 319 | self._cond = ctx.Condition() |
| 320 | |
| 321 | def __getstate__(self): |
| 322 | return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks) |
| 323 | |
| 324 | def __setstate__(self, state): |
| 325 | Queue.__setstate__(self, state[:-2]) |
| 326 | self._cond, self._unfinished_tasks = state[-2:] |
| 327 | |
| 328 | def put(self, obj, block=True, timeout=None): |
| 329 | if self._closed: |
| 330 | raise ValueError(f"Queue {self!r} is closed") |
| 331 | if not self._sem.acquire(block, timeout): |
| 332 | raise Full |
| 333 | |
| 334 | with self._notempty, self._cond: |
| 335 | if self._thread is None: |
| 336 | self._start_thread() |
| 337 | self._buffer.append(obj) |
| 338 | self._unfinished_tasks.release() |
| 339 | self._notempty.notify() |
| 340 | |
| 341 | def task_done(self): |
| 342 | with self._cond: |
| 343 | if not self._unfinished_tasks.acquire(False): |
| 344 | raise ValueError('task_done() called too many times') |
| 345 | if self._unfinished_tasks._semlock._is_zero(): |
| 346 | self._cond.notify_all() |
| 347 | |
| 348 | def join(self): |
| 349 | with self._cond: |
| 350 | if not self._unfinished_tasks._semlock._is_zero(): |
| 351 | self._cond.wait() |
| 352 | |
| 353 | # |
| 354 | # Simplified Queue type -- really just a locked pipe |
no outgoing calls
no test coverage detected
searching dependent graphs…