2 # Support for the API of the multiprocessing package using threads
4 # multiprocessing/dummy/__init__.py
6 # Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
10 'Process', 'current_process', 'active_children', 'freeze_support',
11 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
12 'Event', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
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
35 class DummyProcess(threading
.Thread
):
37 def __init__(self
, group
=None, target
=None, name
=None, args
=(), kwargs
={}):
38 threading
.Thread
.__init
__(self
, group
, target
, name
, args
, kwargs
)
40 self
._children
= weakref
.WeakKeyDictionary()
41 self
._start
_called
= False
42 self
._parent
= current_process()
45 assert self
._parent
is current_process()
46 self
._start
_called
= True
47 self
._parent
._children
[self
] = None
48 threading
.Thread
.start(self
)
52 if self
._start
_called
and not self
.is_alive():
61 class Condition(threading
._Condition
):
62 notify_all
= threading
._Condition
.notify_all
.im_func
68 Process
= DummyProcess
69 current_process
= threading
.current_thread
70 current_process()._children
= weakref
.WeakKeyDictionary()
72 def active_children():
73 children
= current_process()._children
74 for p
in list(children
):
86 class Namespace(object):
87 def __init__(self
, **kwds
):
88 self
.__dict
__.update(kwds
)
90 items
= self
.__dict
__.items()
92 for name
, value
in items
:
93 if not name
.startswith('_'):
94 temp
.append('%s=%r' % (name
, value
))
96 return 'Namespace(%s)' % str.join(', ', temp
)
101 def Array(typecode
, sequence
, lock
=True):
102 return array
.array(typecode
, sequence
)
105 def __init__(self
, typecode
, value
, lock
=True):
106 self
._typecode
= typecode
110 def _set(self
, value
):
112 value
= property(_get
, _set
)
114 return '<%r(%r, %r)>'%(type(self
).__name
__,self
._typecode
,self
._value
)
117 return sys
.modules
[__name__
]
122 def Pool(processes
=None, initializer
=None, initargs
=()):
123 from multiprocessing
.pool
import ThreadPool
124 return ThreadPool(processes
, initializer
, initargs
)
126 JoinableQueue
= Queue