~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Lib/multiprocessing/dummy/connection.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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