Mercurial > cpython-lru_cache-weakref
annotate Lib/multiprocessing/dummy/__init__.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 | ef75eb3dc48e |
| children | 9ac8699d88d6 |
| rev | line source |
|---|---|
|
47694
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
1 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
2 # Support for the API of the multiprocessing package using threads |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
3 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
4 # multiprocessing/dummy/__init__.py |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
5 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
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:
47598
diff
changeset
|
7 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
8 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
9 __all__ = [ |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
10 'Process', 'current_process', 'active_children', 'freeze_support', |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
11 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
12 'Event', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue' |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
13 ] |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
14 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
15 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
16 # Imports |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
17 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
18 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
19 import threading |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
20 import sys |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
21 import weakref |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
22 import array |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
23 import itertools |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
24 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
25 from multiprocessing import TimeoutError, cpu_count |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
26 from multiprocessing.dummy.connection import Pipe |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
27 from threading import Lock, RLock, Semaphore, BoundedSemaphore |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
28 from threading import Event |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
29 from Queue import Queue |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
30 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
31 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
32 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
33 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
34 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
35 class DummyProcess(threading.Thread): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
36 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
37 def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
38 threading.Thread.__init__(self, group, target, name, args, kwargs) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
39 self._pid = None |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
40 self._children = weakref.WeakKeyDictionary() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
41 self._start_called = False |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
42 self._parent = current_process() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
43 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
44 def start(self): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
45 assert self._parent is current_process() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
46 self._start_called = True |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
47 self._parent._children[self] = None |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
48 threading.Thread.start(self) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
49 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
48291
diff
changeset
|
50 @property |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
48291
diff
changeset
|
51 def exitcode(self): |
|
47694
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
52 if self._start_called and not self.is_alive(): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
53 return 0 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
54 else: |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
55 return None |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
56 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
57 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
58 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
59 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
60 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
61 class Condition(threading._Condition): |
|
47826
64abb61e9d4f
get rid of 2.6/3.0 switch statements in multiprocessing
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
62 notify_all = threading._Condition.notify_all.im_func |
|
47694
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
63 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
64 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
65 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
66 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
67 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
68 Process = DummyProcess |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
69 current_process = threading.current_thread |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
70 current_process()._children = weakref.WeakKeyDictionary() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
71 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
72 def active_children(): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
73 children = current_process()._children |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
74 for p in list(children): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
75 if not p.is_alive(): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
76 children.pop(p, None) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
77 return list(children) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
78 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
79 def freeze_support(): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
80 pass |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
81 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
82 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
83 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
84 # |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
85 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
86 class Namespace(object): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
87 def __init__(self, **kwds): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
88 self.__dict__.update(kwds) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
89 def __repr__(self): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
90 items = self.__dict__.items() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
91 temp = [] |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
92 for name, value in items: |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
93 if not name.startswith('_'): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
94 temp.append('%s=%r' % (name, value)) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
95 temp.sort() |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
96 return 'Namespace(%s)' % str.join(', ', temp) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
97 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
98 dict = dict |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
99 list = list |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
100 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
101 def Array(typecode, sequence, lock=True): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
102 return array.array(typecode, sequence) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
103 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
104 class Value(object): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
105 def __init__(self, typecode, value, lock=True): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
106 self._typecode = typecode |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
107 self._value = value |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
108 def _get(self): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
109 return self._value |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
110 def _set(self, value): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
111 self._value = value |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
112 value = property(_get, _set) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
113 def __repr__(self): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
114 return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
115 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
116 def Manager(): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
117 return sys.modules[__name__] |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
118 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
119 def shutdown(): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
120 pass |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
121 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
122 def Pool(processes=None, initializer=None, initargs=()): |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
123 from multiprocessing.pool import ThreadPool |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
124 return ThreadPool(processes, initializer, initargs) |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
125 |
|
af8873df0322
convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
126 JoinableQueue = Queue |
