annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/multiprocessing/dummy/connection.py @ 68:5028fdace37b

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