10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Support for the API of the multiprocessing package using threads 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# multiprocessing/dummy/__init__.py 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Copyright (c) 2006-2008, R Oudkerk 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# All rights reserved. 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Redistribution and use in source and binary forms, with or without 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# modification, are permitted provided that the following conditions 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# are met: 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1. Redistributions of source code must retain the above copyright 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# notice, this list of conditions and the following disclaimer. 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 2. Redistributions in binary form must reproduce the above copyright 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# notice, this list of conditions and the following disclaimer in the 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# documentation and/or other materials provided with the distribution. 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 3. Neither the name of author nor the names of any contributors may be 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# used to endorse or promote products derived from this software 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# without specific prior written permission. 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# SUCH DAMAGE. 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__all__ = [ 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Process', 'current_process', 'active_children', 'freeze_support', 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Event', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue' 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ] 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Imports 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport threading 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport weakref 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport array 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport itertools 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom multiprocessing import TimeoutError, cpu_count 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom multiprocessing.dummy.connection import Pipe 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom threading import Lock, RLock, Semaphore, BoundedSemaphore 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom threading import Event 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom Queue import Queue 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass DummyProcess(threading.Thread): 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao threading.Thread.__init__(self, group, target, name, args, kwargs) 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._pid = None 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._children = weakref.WeakKeyDictionary() 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._start_called = False 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._parent = current_process() 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def start(self): 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert self._parent is current_process() 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._start_called = True 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(self._parent, '_children'): 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._parent._children[self] = None 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao threading.Thread.start(self) 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao @property 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def exitcode(self): 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._start_called and not self.is_alive(): 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Condition(threading._Condition): 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao notify_all = threading._Condition.notify_all.im_func 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 950a8c90248264a8b26970b4473770bcc3df8515fJosh GaoProcess = DummyProcess 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocurrent_process = threading.current_thread 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocurrent_process()._children = weakref.WeakKeyDictionary() 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef active_children(): 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao children = current_process()._children 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for p in list(children): 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not p.is_alive(): 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao children.pop(p, None) 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return list(children) 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef freeze_support(): 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Namespace(object): 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, **kwds): 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.__dict__.update(kwds) 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao items = self.__dict__.items() 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao temp = [] 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for name, value in items: 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not name.startswith('_'): 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao temp.append('%s=%r' % (name, value)) 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao temp.sort() 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 'Namespace(%s)' % str.join(', ', temp) 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodict = dict 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaolist = list 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef Array(typecode, sequence, lock=True): 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return array.array(typecode, sequence) 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Value(object): 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, typecode, value, lock=True): 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._typecode = typecode 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._value = value 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _get(self): 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._value 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _set(self, value): 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._value = value 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value = property(_get, _set) 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value) 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef Manager(): 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sys.modules[__name__] 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef shutdown(): 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef Pool(processes=None, initializer=None, initargs=()): 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from multiprocessing.pool import ThreadPool 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ThreadPool(processes, initializer, initargs) 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1530a8c90248264a8b26970b4473770bcc3df8515fJosh GaoJoinableQueue = Queue 154