PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/Lib/multiprocessing/dummy/connection.py

http://unladen-swallow.googlecode.com/
Python | 61 lines | 35 code | 19 blank | 7 comment | 2 complexity | 5e745d77e920d275d885bff91317bb89 MD5 | raw file
 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
11from Queue import Queue
12
13
14families = [None]
15
16
17class 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
31def Client(address):
32    _in, _out = Queue(), Queue()
33    address.put((_out, _in))
34    return Connection(_in, _out)
35
36
37def Pipe(duplex=True):
38    a, b = Queue(), Queue()
39    return Connection(a, b), Connection(b, a)
40
41
42class 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