Benjamin Peterson | 190d56e | 2008-06-11 02:40:25 +0000 | [diff] [blame] | 1 | #
|
| 2 | # Analogue of `multiprocessing.connection` which uses queues instead of sockets
|
| 3 | #
|
| 4 | # multiprocessing/dummy/connection.py
|
| 5 | #
|
| 6 | # Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
|
| 7 | #
|
| 8 |
|
| 9 | __all__ = [ 'Client', 'Listener', 'Pipe' ]
|
| 10 |
|
| 11 | from Queue import Queue
|
| 12 |
|
| 13 |
|
| 14 | families = [None]
|
| 15 |
|
| 16 |
|
| 17 | class Listener(object):
|
| 18 |
|
| 19 | def __init__(self, address=None, family=None, backlog=1):
|
| 20 | self._backlog_queue = Queue(backlog)
|
| 21 |
|
| 22 | def accept(self):
|
| 23 | return Connection(*self._backlog_queue.get())
|
| 24 |
|
| 25 | def close(self):
|
| 26 | self._backlog_queue = None
|
| 27 |
|
| 28 | address = property(lambda self: self._backlog_queue)
|
| 29 |
|
| 30 |
|
| 31 | def Client(address):
|
| 32 | _in, _out = Queue(), Queue()
|
| 33 | address.put((_out, _in))
|
| 34 | return Connection(_in, _out)
|
| 35 |
|
| 36 |
|
| 37 | def Pipe(duplex=True):
|
| 38 | a, b = Queue(), Queue()
|
| 39 | return Connection(a, b), Connection(b, a)
|
| 40 |
|
| 41 |
|
| 42 | class Connection(object):
|
| 43 |
|
| 44 | def __init__(self, _in, _out):
|
| 45 | self._out = _out
|
| 46 | self._in = _in
|
| 47 | self.send = self.send_bytes = _out.put
|
| 48 | self.recv = self.recv_bytes = _in.get
|
| 49 |
|
| 50 | def poll(self, timeout=0.0):
|
| 51 | if self._in.qsize() > 0:
|
| 52 | return True
|
| 53 | if timeout <= 0.0:
|
| 54 | return False
|
| 55 | self._in.not_empty.acquire()
|
| 56 | self._in.not_empty.wait(timeout)
|
| 57 | self._in.not_empty.release()
|
| 58 | return self._in.qsize() > 0
|
| 59 |
|
| 60 | def close(self):
|
| 61 | pass
|