Back to index

python3.2  3.2.2
__init__.py
Go to the documentation of this file.
00001 #
00002 # Support for the API of the multiprocessing package using threads
00003 #
00004 # multiprocessing/dummy/__init__.py
00005 #
00006 # Copyright (c) 2006-2008, R Oudkerk
00007 # All rights reserved.
00008 #
00009 # Redistribution and use in source and binary forms, with or without
00010 # modification, are permitted provided that the following conditions
00011 # are met:
00012 #
00013 # 1. Redistributions of source code must retain the above copyright
00014 #    notice, this list of conditions and the following disclaimer.
00015 # 2. Redistributions in binary form must reproduce the above copyright
00016 #    notice, this list of conditions and the following disclaimer in the
00017 #    documentation and/or other materials provided with the distribution.
00018 # 3. Neither the name of author nor the names of any contributors may be
00019 #    used to endorse or promote products derived from this software
00020 #    without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
00023 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00026 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032 # SUCH DAMAGE.
00033 #
00034 
00035 __all__ = [
00036     'Process', 'current_process', 'active_children', 'freeze_support',
00037     'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
00038     'Event', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
00039     ]
00040 
00041 #
00042 # Imports
00043 #
00044 
00045 import threading
00046 import sys
00047 import weakref
00048 import array
00049 import itertools
00050 
00051 from multiprocessing import TimeoutError, cpu_count
00052 from multiprocessing.dummy.connection import Pipe
00053 from threading import Lock, RLock, Semaphore, BoundedSemaphore
00054 from threading import Event
00055 from queue import Queue
00056 
00057 #
00058 #
00059 #
00060 
00061 class DummyProcess(threading.Thread):
00062 
00063     def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
00064         threading.Thread.__init__(self, group, target, name, args, kwargs)
00065         self._pid = None
00066         self._children = weakref.WeakKeyDictionary()
00067         self._start_called = False
00068         self._parent = current_process()
00069 
00070     def start(self):
00071         assert self._parent is current_process()
00072         self._start_called = True
00073         self._parent._children[self] = None
00074         threading.Thread.start(self)
00075 
00076     @property
00077     def exitcode(self):
00078         if self._start_called and not self.is_alive():
00079             return 0
00080         else:
00081             return None
00082 
00083 #
00084 #
00085 #
00086 
00087 class Condition(threading._Condition):
00088     # XXX
00089     if sys.version_info < (3, 0):
00090         notify_all = threading._Condition.notify_all.__func__
00091     else:
00092         notify_all = threading._Condition.notify_all
00093 
00094 #
00095 #
00096 #
00097 
00098 Process = DummyProcess
00099 current_process = threading.current_thread
00100 current_process()._children = weakref.WeakKeyDictionary()
00101 
00102 def active_children():
00103     children = current_process()._children
00104     for p in list(children):
00105         if not p.is_alive():
00106             children.pop(p, None)
00107     return list(children)
00108 
00109 def freeze_support():
00110     pass
00111 
00112 #
00113 #
00114 #
00115 
00116 class Namespace(object):
00117     def __init__(self, **kwds):
00118         self.__dict__.update(kwds)
00119     def __repr__(self):
00120         items = list(self.__dict__.items())
00121         temp = []
00122         for name, value in items:
00123             if not name.startswith('_'):
00124                 temp.append('%s=%r' % (name, value))
00125         temp.sort()
00126         return 'Namespace(%s)' % str.join(', ', temp)
00127 
00128 dict = dict
00129 list = list
00130 
00131 def Array(typecode, sequence, lock=True):
00132     return array.array(typecode, sequence)
00133 
00134 class Value(object):
00135     def __init__(self, typecode, value, lock=True):
00136         self._typecode = typecode
00137         self._value = value
00138     def _get(self):
00139         return self._value
00140     def _set(self, value):
00141         self._value = value
00142     value = property(_get, _set)
00143     def __repr__(self):
00144         return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value)
00145 
00146 def Manager():
00147     return sys.modules[__name__]
00148 
00149 def shutdown():
00150     pass
00151 
00152 def Pool(processes=None, initializer=None, initargs=()):
00153     from multiprocessing.pool import ThreadPool
00154     return ThreadPool(processes, initializer, initargs)
00155 
00156 JoinableQueue = Queue