2 # Analogue of `multiprocessing.connection` which uses queues instead of sockets
4 # multiprocessing/dummy/connection.py
6 # Copyright (c) 2006-2008, R Oudkerk
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 # 3. Neither the name of author nor the names of any contributors may be
19 # used to endorse or promote products derived from this software
20 # without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
23 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 __all__ = [ 'Client', 'Listener', 'Pipe' ]
37 from Queue import Queue
43 class Listener(object):
45 def __init__(self, address=None, family=None, backlog=1):
46 self._backlog_queue = Queue(backlog)
49 return Connection(*self._backlog_queue.get())
52 self._backlog_queue = None
54 address = property(lambda self: self._backlog_queue)
58 _in, _out = Queue(), Queue()
59 address.put((_out, _in))
60 return Connection(_in, _out)
63 def Pipe(duplex=True):
64 a, b = Queue(), Queue()
65 return Connection(a, b), Connection(b, a)
68 class Connection(object):
70 def __init__(self, _in, _out):
73 self.send = self.send_bytes = _out.put
74 self.recv = self.recv_bytes = _in.get
76 def poll(self, timeout=0.0):
77 if self._in.qsize() > 0:
81 self._in.not_empty.acquire()
82 self._in.not_empty.wait(timeout)
83 self._in.not_empty.release()
84 return self._in.qsize() > 0