annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/multiprocessing/dummy/connection.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 #
jpayne@69 2 # Analogue of `multiprocessing.connection` which uses queues instead of sockets
jpayne@69 3 #
jpayne@69 4 # multiprocessing/dummy/connection.py
jpayne@69 5 #
jpayne@69 6 # Copyright (c) 2006-2008, R Oudkerk
jpayne@69 7 # Licensed to PSF under a Contributor Agreement.
jpayne@69 8 #
jpayne@69 9
jpayne@69 10 __all__ = [ 'Client', 'Listener', 'Pipe' ]
jpayne@69 11
jpayne@69 12 from queue import Queue
jpayne@69 13
jpayne@69 14
jpayne@69 15 families = [None]
jpayne@69 16
jpayne@69 17
jpayne@69 18 class Listener(object):
jpayne@69 19
jpayne@69 20 def __init__(self, address=None, family=None, backlog=1):
jpayne@69 21 self._backlog_queue = Queue(backlog)
jpayne@69 22
jpayne@69 23 def accept(self):
jpayne@69 24 return Connection(*self._backlog_queue.get())
jpayne@69 25
jpayne@69 26 def close(self):
jpayne@69 27 self._backlog_queue = None
jpayne@69 28
jpayne@69 29 @property
jpayne@69 30 def address(self):
jpayne@69 31 return self._backlog_queue
jpayne@69 32
jpayne@69 33 def __enter__(self):
jpayne@69 34 return self
jpayne@69 35
jpayne@69 36 def __exit__(self, exc_type, exc_value, exc_tb):
jpayne@69 37 self.close()
jpayne@69 38
jpayne@69 39
jpayne@69 40 def Client(address):
jpayne@69 41 _in, _out = Queue(), Queue()
jpayne@69 42 address.put((_out, _in))
jpayne@69 43 return Connection(_in, _out)
jpayne@69 44
jpayne@69 45
jpayne@69 46 def Pipe(duplex=True):
jpayne@69 47 a, b = Queue(), Queue()
jpayne@69 48 return Connection(a, b), Connection(b, a)
jpayne@69 49
jpayne@69 50
jpayne@69 51 class Connection(object):
jpayne@69 52
jpayne@69 53 def __init__(self, _in, _out):
jpayne@69 54 self._out = _out
jpayne@69 55 self._in = _in
jpayne@69 56 self.send = self.send_bytes = _out.put
jpayne@69 57 self.recv = self.recv_bytes = _in.get
jpayne@69 58
jpayne@69 59 def poll(self, timeout=0.0):
jpayne@69 60 if self._in.qsize() > 0:
jpayne@69 61 return True
jpayne@69 62 if timeout <= 0.0:
jpayne@69 63 return False
jpayne@69 64 with self._in.not_empty:
jpayne@69 65 self._in.not_empty.wait(timeout)
jpayne@69 66 return self._in.qsize() > 0
jpayne@69 67
jpayne@69 68 def close(self):
jpayne@69 69 pass
jpayne@69 70
jpayne@69 71 def __enter__(self):
jpayne@69 72 return self
jpayne@69 73
jpayne@69 74 def __exit__(self, exc_type, exc_value, exc_tb):
jpayne@69 75 self.close()