PageRenderTime 359ms CodeModel.GetById 141ms app.highlight 10ms RepoModel.GetById 206ms app.codeStats 0ms

/Lib/multiprocessing/dummy/__init__.py

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