~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Lib/multiprocessing/dummy/__init__.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
import threading
 
20
import sys
 
21
import weakref
 
22
import array
 
23
import itertools
 
24
 
 
25
from multiprocessing import TimeoutError, cpu_count
 
26
from multiprocessing.dummy.connection import Pipe
 
27
from threading import Lock, RLock, Semaphore, BoundedSemaphore
 
28
from threading import Event
 
29
from Queue import Queue
 
30
 
 
31
#
 
32
#
 
33
#
 
34
 
 
35
class 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
    is_alive = threading.Thread.is_alive.im_func
 
58
    get_name = threading.Thread.getName.im_func
 
59
    set_name = threading.Thread.setName.im_func
 
60
    is_daemon = threading.Thread.isDaemon.im_func
 
61
    set_daemon = threading.Thread.setDaemon.im_func
 
62
 
 
63
#
 
64
#
 
65
#
 
66
 
 
67
class Condition(threading._Condition):
 
68
    notify_all = threading._Condition.notify_all.im_func
 
69
 
 
70
#
 
71
#
 
72
#
 
73
 
 
74
Process = DummyProcess
 
75
current_process = threading.current_thread
 
76
current_process()._children = weakref.WeakKeyDictionary()
 
77
 
 
78
def active_children():
 
79
    children = current_process()._children
 
80
    for p in list(children):
 
81
        if not p.is_alive():
 
82
            children.pop(p, None)
 
83
    return list(children)
 
84
 
 
85
def freeze_support():
 
86
    pass
 
87
 
 
88
#
 
89
#
 
90
#
 
91
 
 
92
class Namespace(object):
 
93
    def __init__(self, **kwds):
 
94
        self.__dict__.update(kwds)
 
95
    def __repr__(self):
 
96
        items = self.__dict__.items()
 
97
        temp = []
 
98
        for name, value in items:
 
99
            if not name.startswith('_'):
 
100
                temp.append('%s=%r' % (name, value))
 
101
        temp.sort()
 
102
        return 'Namespace(%s)' % str.join(', ', temp)
 
103
 
 
104
dict = dict
 
105
list = list
 
106
 
 
107
def Array(typecode, sequence, lock=True):
 
108
    return array.array(typecode, sequence)
 
109
 
 
110
class Value(object):
 
111
    def __init__(self, typecode, value, lock=True):
 
112
        self._typecode = typecode
 
113
        self._value = value
 
114
    def _get(self):
 
115
        return self._value
 
116
    def _set(self, value):
 
117
        self._value = value
 
118
    value = property(_get, _set)
 
119
    def __repr__(self):
 
120
        return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value)
 
121
 
 
122
def Manager():
 
123
    return sys.modules[__name__]
 
124
 
 
125
def shutdown():
 
126
    pass
 
127
 
 
128
def Pool(processes=None, initializer=None, initargs=()):
 
129
    from multiprocessing.pool import ThreadPool
 
130
    return ThreadPool(processes, initializer, initargs)
 
131
 
 
132
JoinableQueue = Queue