30b1b20f56af2f690811398a9e7226a6dbb48f64
[profile/ivi/python.git] / Lib / multiprocessing / dummy / __init__.py
1 #
2 # Support for the API of the multiprocessing package using threads
3 #
4 # multiprocessing/dummy/__init__.py
5 #
6 # Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
7 #
8
9 __all__ = [
10     'Process', 'current_process', 'active_children', 'freeze_support',
11     'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
12     'Event', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
13     ]
14
15 #
16 # Imports
17 #
18
19 import threading
20 import sys
21 import weakref
22 import array
23 import itertools
24
25 from multiprocessing import TimeoutError, cpu_count
26 from multiprocessing.dummy.connection import Pipe
27 from threading import Lock, RLock, Semaphore, BoundedSemaphore
28 from threading import Event
29 from Queue import Queue
30
31 #
32 #
33 #
34
35 class DummyProcess(threading.Thread):
36
37     def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
38         threading.Thread.__init__(self, group, target, name, args, kwargs)
39         self._pid = None
40         self._children = weakref.WeakKeyDictionary()
41         self._start_called = False
42         self._parent = current_process()
43
44     def start(self):
45         assert self._parent is current_process()
46         self._start_called = True
47         self._parent._children[self] = None
48         threading.Thread.start(self)
49
50     @property
51     def exitcode(self):
52         if self._start_called and not self.is_alive():
53             return 0
54         else:
55             return None
56
57 #
58 #
59 #
60
61 class Condition(threading._Condition):
62     notify_all = threading._Condition.notify_all.im_func
63
64 #
65 #
66 #
67
68 Process = DummyProcess
69 current_process = threading.current_thread
70 current_process()._children = weakref.WeakKeyDictionary()
71
72 def active_children():
73     children = current_process()._children
74     for p in list(children):
75         if not p.is_alive():
76             children.pop(p, None)
77     return list(children)
78
79 def freeze_support():
80     pass
81
82 #
83 #
84 #
85
86 class Namespace(object):
87     def __init__(self, **kwds):
88         self.__dict__.update(kwds)
89     def __repr__(self):
90         items = self.__dict__.items()
91         temp = []
92         for name, value in items:
93             if not name.startswith('_'):
94                 temp.append('%s=%r' % (name, value))
95         temp.sort()
96         return 'Namespace(%s)' % str.join(', ', temp)
97
98 dict = dict
99 list = list
100
101 def Array(typecode, sequence, lock=True):
102     return array.array(typecode, sequence)
103
104 class Value(object):
105     def __init__(self, typecode, value, lock=True):
106         self._typecode = typecode
107         self._value = value
108     def _get(self):
109         return self._value
110     def _set(self, value):
111         self._value = value
112     value = property(_get, _set)
113     def __repr__(self):
114         return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value)
115
116 def Manager():
117     return sys.modules[__name__]
118
119 def shutdown():
120     pass
121
122 def Pool(processes=None, initializer=None, initargs=()):
123     from multiprocessing.pool import ThreadPool
124     return ThreadPool(processes, initializer, initargs)
125
126 JoinableQueue = Queue