blob: 20ae957b80392ecb917389a4b762e2b4da8d8a3f [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001#
2# Support for the API of the multiprocessing package using threads
3#
4# multiprocessing/dummy/__init__.py
5#
R. David Murray3fc969a2010-12-14 01:38:16 +00006# Copyright (c) 2006-2008, R Oudkerk
Richard Oudkerkef453802013-01-01 14:25:59 +00007# Licensed to PSF under a Contributor Agreement.
Benjamin Petersone711caf2008-06-11 16:44:04 +00008#
9
10__all__ = [
11 'Process', 'current_process', 'active_children', 'freeze_support',
12 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
Richard Oudkerk3730a172012-06-15 18:26:07 +010013 'Event', 'Barrier', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
Benjamin Petersone711caf2008-06-11 16:44:04 +000014 ]
15
16#
17# Imports
18#
19
20import threading
21import sys
22import weakref
23import array
Benjamin Petersone711caf2008-06-11 16:44:04 +000024
Benjamin Petersone711caf2008-06-11 16:44:04 +000025from multiprocessing.dummy.connection import Pipe
26from threading import Lock, RLock, Semaphore, BoundedSemaphore
Richard Oudkerk3730a172012-06-15 18:26:07 +010027from threading import Event, Condition, Barrier
Benjamin Petersone711caf2008-06-11 16:44:04 +000028from 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
Richard Oudkerk54454e72012-05-25 12:57:58 +010046 if hasattr(self._parent, '_children'):
47 self._parent._children[self] = None
Benjamin Petersone711caf2008-06-11 16:44:04 +000048 threading.Thread.start(self)
49
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +000050 @property
51 def exitcode(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +000052 if self._start_called and not self.is_alive():
Benjamin Petersone711caf2008-06-11 16:44:04 +000053 return 0
54 else:
55 return None
56
Benjamin Petersone711caf2008-06-11 16:44:04 +000057#
58#
59#
60
Benjamin Petersone711caf2008-06-11 16:44:04 +000061Process = DummyProcess
Benjamin Peterson672b8032008-06-11 19:14:14 +000062current_process = threading.current_thread
Benjamin Petersone711caf2008-06-11 16:44:04 +000063current_process()._children = weakref.WeakKeyDictionary()
64
65def active_children():
66 children = current_process()._children
67 for p in list(children):
Benjamin Peterson672b8032008-06-11 19:14:14 +000068 if not p.is_alive():
Benjamin Petersone711caf2008-06-11 16:44:04 +000069 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 'Namespace(%s)' % str.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 '<%r(%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 multiprocessing.pool import ThreadPool
117 return ThreadPool(processes, initializer, initargs)
118
119JoinableQueue = Queue