blob: e31fc61572a430000bcf9dad477b8652b4cf1f91 [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001#
2# Support for the API of the multiprocessing package using threads
3#
4# multiprocessing/dummy/__init__.py
5#
R. David Murray3fc969a2010-12-14 01:38:16 +00006# Copyright (c) 2006-2008, R Oudkerk
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12#
13# 1. Redistributions of source code must retain the above copyright
14# notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16# notice, this list of conditions and the following disclaimer in the
17# documentation and/or other materials provided with the distribution.
18# 3. Neither the name of author nor the names of any contributors may be
19# used to endorse or promote products derived from this software
20# without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
23# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32# SUCH DAMAGE.
Benjamin Petersone711caf2008-06-11 16:44:04 +000033#
34
35__all__ = [
36 'Process', 'current_process', 'active_children', 'freeze_support',
37 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
Richard Oudkerk3730a172012-06-15 18:26:07 +010038 'Event', 'Barrier', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
Benjamin Petersone711caf2008-06-11 16:44:04 +000039 ]
40
41#
42# Imports
43#
44
45import threading
46import sys
47import weakref
48import array
Benjamin Petersone711caf2008-06-11 16:44:04 +000049
Benjamin Petersone711caf2008-06-11 16:44:04 +000050from multiprocessing.dummy.connection import Pipe
51from threading import Lock, RLock, Semaphore, BoundedSemaphore
Richard Oudkerk3730a172012-06-15 18:26:07 +010052from threading import Event, Condition, Barrier
Benjamin Petersone711caf2008-06-11 16:44:04 +000053from queue import Queue
54
55#
56#
57#
58
59class DummyProcess(threading.Thread):
60
61 def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
62 threading.Thread.__init__(self, group, target, name, args, kwargs)
63 self._pid = None
64 self._children = weakref.WeakKeyDictionary()
65 self._start_called = False
66 self._parent = current_process()
67
68 def start(self):
69 assert self._parent is current_process()
70 self._start_called = True
Richard Oudkerk54454e72012-05-25 12:57:58 +010071 if hasattr(self._parent, '_children'):
72 self._parent._children[self] = None
Benjamin Petersone711caf2008-06-11 16:44:04 +000073 threading.Thread.start(self)
74
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +000075 @property
76 def exitcode(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +000077 if self._start_called and not self.is_alive():
Benjamin Petersone711caf2008-06-11 16:44:04 +000078 return 0
79 else:
80 return None
81
Benjamin Petersone711caf2008-06-11 16:44:04 +000082#
83#
84#
85
Benjamin Petersone711caf2008-06-11 16:44:04 +000086Process = DummyProcess
Benjamin Peterson672b8032008-06-11 19:14:14 +000087current_process = threading.current_thread
Benjamin Petersone711caf2008-06-11 16:44:04 +000088current_process()._children = weakref.WeakKeyDictionary()
89
90def active_children():
91 children = current_process()._children
92 for p in list(children):
Benjamin Peterson672b8032008-06-11 19:14:14 +000093 if not p.is_alive():
Benjamin Petersone711caf2008-06-11 16:44:04 +000094 children.pop(p, None)
95 return list(children)
96
97def freeze_support():
98 pass
99
100#
101#
102#
103
104class Namespace(object):
105 def __init__(self, **kwds):
106 self.__dict__.update(kwds)
107 def __repr__(self):
108 items = list(self.__dict__.items())
109 temp = []
110 for name, value in items:
111 if not name.startswith('_'):
112 temp.append('%s=%r' % (name, value))
113 temp.sort()
114 return 'Namespace(%s)' % str.join(', ', temp)
115
116dict = dict
117list = list
118
119def Array(typecode, sequence, lock=True):
120 return array.array(typecode, sequence)
121
122class Value(object):
123 def __init__(self, typecode, value, lock=True):
124 self._typecode = typecode
125 self._value = value
126 def _get(self):
127 return self._value
128 def _set(self, value):
129 self._value = value
130 value = property(_get, _set)
131 def __repr__(self):
132 return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value)
133
134def Manager():
135 return sys.modules[__name__]
136
137def shutdown():
138 pass
139
140def Pool(processes=None, initializer=None, initargs=()):
141 from multiprocessing.pool import ThreadPool
142 return ThreadPool(processes, initializer, initargs)
143
144JoinableQueue = Queue