Mercurial > cpython-lru_cache-weakref
annotate Lib/multiprocessing/dummy/connection.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | af8873df0322 |
| children | 9ac8699d88d6 |
| rev | line source |
|---|---|
|
47694
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
1 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
2 # Analogue of `multiprocessing.connection` which uses queues instead of sockets |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
3 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
4 # multiprocessing/dummy/connection.py |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
5 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
6 # Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
7 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
8 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
9 __all__ = [ 'Client', 'Listener', 'Pipe' ] |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
10 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
11 from Queue import Queue |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
12 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
13 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
14 families = [None] |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
15 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
16 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
17 class Listener(object): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
18 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
19 def __init__(self, address=None, family=None, backlog=1): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
20 self._backlog_queue = Queue(backlog) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
21 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
22 def accept(self): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
23 return Connection(*self._backlog_queue.get()) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
24 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
25 def close(self): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
26 self._backlog_queue = None |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
27 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
28 address = property(lambda self: self._backlog_queue) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
29 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
30 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
31 def Client(address): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
32 _in, _out = Queue(), Queue() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
33 address.put((_out, _in)) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
34 return Connection(_in, _out) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
35 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
36 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
37 def Pipe(duplex=True): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
38 a, b = Queue(), Queue() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
39 return Connection(a, b), Connection(b, a) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
40 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
41 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
42 class Connection(object): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
43 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
44 def __init__(self, _in, _out): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
45 self._out = _out |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
46 self._in = _in |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
47 self.send = self.send_bytes = _out.put |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
48 self.recv = self.recv_bytes = _in.get |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
49 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
50 def poll(self, timeout=0.0): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
51 if self._in.qsize() > 0: |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
52 return True |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
53 if timeout <= 0.0: |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
54 return False |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
55 self._in.not_empty.acquire() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
56 self._in.not_empty.wait(timeout) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
57 self._in.not_empty.release() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
58 return self._in.qsize() > 0 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
59 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
60 def close(self): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47577
diff
changeset
|
61 pass |
