• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7# Licensed to PSF under a Contributor Agreement.
8#
9
10__all__ = [
11    'Process', 'current_process', 'active_children', 'freeze_support',
12    'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
13    'Event', 'Barrier', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
14    ]
15
16#
17# Imports
18#
19
20import threading
21import sys
22import weakref
23import array
24
25from .connection import Pipe
26from threading import Lock, RLock, Semaphore, BoundedSemaphore
27from threading import Event, Condition, Barrier
28from queue import Queue
29
30#
31#
32#
33
34class DummyProcess(threading.Thread):
35
36    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
37        threading.Thread.__init__(self, group, target, name, args, kwargs)
38        self._pid = None
39        self._children = weakref.WeakKeyDictionary()
40        self._start_called = False
41        self._parent = current_process()
42
43    def start(self):
44        assert self._parent is current_process()
45        self._start_called = True
46        if hasattr(self._parent, '_children'):
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
61Process = DummyProcess
62current_process = threading.current_thread
63current_process()._children = weakref.WeakKeyDictionary()
64
65def active_children():
66    children = current_process()._children
67    for p in list(children):
68        if not p.is_alive():
69            children.pop(p, None)
70    return list(children)
71
72def freeze_support():
73    pass
74
75#
76#
77#
78
79class Namespace(object):
80    def __init__(self, **kwds):
81        self.__dict__.update(kwds)
82    def __repr__(self):
83        items = list(self.__dict__.items())
84        temp = []
85        for name, value in items:
86            if not name.startswith('_'):
87                temp.append('%s=%r' % (name, value))
88        temp.sort()
89        return '%s(%s)' % (self.__class__.__name__, ', '.join(temp))
90
91dict = dict
92list = list
93
94def Array(typecode, sequence, lock=True):
95    return array.array(typecode, sequence)
96
97class Value(object):
98    def __init__(self, typecode, value, lock=True):
99        self._typecode = typecode
100        self._value = value
101    def _get(self):
102        return self._value
103    def _set(self, value):
104        self._value = value
105    value = property(_get, _set)
106    def __repr__(self):
107        return '<%s(%r, %r)>'%(type(self).__name__,self._typecode,self._value)
108
109def Manager():
110    return sys.modules[__name__]
111
112def shutdown():
113    pass
114
115def Pool(processes=None, initializer=None, initargs=()):
116    from ..pool import ThreadPool
117    return ThreadPool(processes, initializer, initargs)
118
119JoinableQueue = Queue
120