Update to 2.7.3
[profile/ivi/python.git] / Lib / multiprocessing / dummy / connection.py
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
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
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.
21 #
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
32 # SUCH DAMAGE.
33 #
34
35 __all__ = [ 'Client', 'Listener', 'Pipe' ]
36
37 from Queue import Queue
38
39
40 families = [None]
41
42
43 class Listener(object):
44
45     def __init__(self, address=None, family=None, backlog=1):
46         self._backlog_queue = Queue(backlog)
47
48     def accept(self):
49         return Connection(*self._backlog_queue.get())
50
51     def close(self):
52         self._backlog_queue = None
53
54     address = property(lambda self: self._backlog_queue)
55
56
57 def Client(address):
58     _in, _out = Queue(), Queue()
59     address.put((_out, _in))
60     return Connection(_in, _out)
61
62
63 def Pipe(duplex=True):
64     a, b = Queue(), Queue()
65     return Connection(a, b), Connection(b, a)
66
67
68 class Connection(object):
69
70     def __init__(self, _in, _out):
71         self._out = _out
72         self._in = _in
73         self.send = self.send_bytes = _out.put
74         self.recv = self.recv_bytes = _in.get
75
76     def poll(self, timeout=0.0):
77         if self._in.qsize() > 0:
78             return True
79         if timeout <= 0.0:
80             return False
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
85
86     def close(self):
87         pass