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