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