Some platforms have rl_completion_append_character but not rl_completion_suppress_append.
[python.git] / Doc / includes / mp_synchronize.py
blobfd2ae77d870fafae321a381c1f859a5b903f2e3f
2 # A test file for the `multiprocessing` package
4 # Copyright (c) 2006-2008, R Oudkerk
5 # All rights reserved.
8 import time, sys, random
9 from Queue import Empty
11 import multiprocessing # may get overwritten
14 #### TEST_VALUE
16 def value_func(running, mutex):
17 random.seed()
18 time.sleep(random.random()*4)
20 mutex.acquire()
21 print '\n\t\t\t' + str(multiprocessing.current_process()) + ' has finished'
22 running.value -= 1
23 mutex.release()
25 def test_value():
26 TASKS = 10
27 running = multiprocessing.Value('i', TASKS)
28 mutex = multiprocessing.Lock()
30 for i in range(TASKS):
31 p = multiprocessing.Process(target=value_func, args=(running, mutex))
32 p.start()
34 while running.value > 0:
35 time.sleep(0.08)
36 mutex.acquire()
37 print running.value,
38 sys.stdout.flush()
39 mutex.release()
41 print
42 print 'No more running processes'
45 #### TEST_QUEUE
47 def queue_func(queue):
48 for i in range(30):
49 time.sleep(0.5 * random.random())
50 queue.put(i*i)
51 queue.put('STOP')
53 def test_queue():
54 q = multiprocessing.Queue()
56 p = multiprocessing.Process(target=queue_func, args=(q,))
57 p.start()
59 o = None
60 while o != 'STOP':
61 try:
62 o = q.get(timeout=0.3)
63 print o,
64 sys.stdout.flush()
65 except Empty:
66 print 'TIMEOUT'
68 print
71 #### TEST_CONDITION
73 def condition_func(cond):
74 cond.acquire()
75 print '\t' + str(cond)
76 time.sleep(2)
77 print '\tchild is notifying'
78 print '\t' + str(cond)
79 cond.notify()
80 cond.release()
82 def test_condition():
83 cond = multiprocessing.Condition()
85 p = multiprocessing.Process(target=condition_func, args=(cond,))
86 print cond
88 cond.acquire()
89 print cond
90 cond.acquire()
91 print cond
93 p.start()
95 print 'main is waiting'
96 cond.wait()
97 print 'main has woken up'
99 print cond
100 cond.release()
101 print cond
102 cond.release()
104 p.join()
105 print cond
108 #### TEST_SEMAPHORE
110 def semaphore_func(sema, mutex, running):
111 sema.acquire()
113 mutex.acquire()
114 running.value += 1
115 print running.value, 'tasks are running'
116 mutex.release()
118 random.seed()
119 time.sleep(random.random()*2)
121 mutex.acquire()
122 running.value -= 1
123 print '%s has finished' % multiprocessing.current_process()
124 mutex.release()
126 sema.release()
128 def test_semaphore():
129 sema = multiprocessing.Semaphore(3)
130 mutex = multiprocessing.RLock()
131 running = multiprocessing.Value('i', 0)
133 processes = [
134 multiprocessing.Process(target=semaphore_func,
135 args=(sema, mutex, running))
136 for i in range(10)
139 for p in processes:
140 p.start()
142 for p in processes:
143 p.join()
146 #### TEST_JOIN_TIMEOUT
148 def join_timeout_func():
149 print '\tchild sleeping'
150 time.sleep(5.5)
151 print '\n\tchild terminating'
153 def test_join_timeout():
154 p = multiprocessing.Process(target=join_timeout_func)
155 p.start()
157 print 'waiting for process to finish'
159 while 1:
160 p.join(timeout=1)
161 if not p.is_alive():
162 break
163 print '.',
164 sys.stdout.flush()
167 #### TEST_EVENT
169 def event_func(event):
170 print '\t%r is waiting' % multiprocessing.current_process()
171 event.wait()
172 print '\t%r has woken up' % multiprocessing.current_process()
174 def test_event():
175 event = multiprocessing.Event()
177 processes = [multiprocessing.Process(target=event_func, args=(event,))
178 for i in range(5)]
180 for p in processes:
181 p.start()
183 print 'main is sleeping'
184 time.sleep(2)
186 print 'main is setting event'
187 event.set()
189 for p in processes:
190 p.join()
193 #### TEST_SHAREDVALUES
195 def sharedvalues_func(values, arrays, shared_values, shared_arrays):
196 for i in range(len(values)):
197 v = values[i][1]
198 sv = shared_values[i].value
199 assert v == sv
201 for i in range(len(values)):
202 a = arrays[i][1]
203 sa = list(shared_arrays[i][:])
204 assert a == sa
206 print 'Tests passed'
208 def test_sharedvalues():
209 values = [
210 ('i', 10),
211 ('h', -2),
212 ('d', 1.25)
214 arrays = [
215 ('i', range(100)),
216 ('d', [0.25 * i for i in range(100)]),
217 ('H', range(1000))
220 shared_values = [multiprocessing.Value(id, v) for id, v in values]
221 shared_arrays = [multiprocessing.Array(id, a) for id, a in arrays]
223 p = multiprocessing.Process(
224 target=sharedvalues_func,
225 args=(values, arrays, shared_values, shared_arrays)
227 p.start()
228 p.join()
230 assert p.exitcode == 0
233 ####
235 def test(namespace=multiprocessing):
236 global multiprocessing
238 multiprocessing = namespace
240 for func in [ test_value, test_queue, test_condition,
241 test_semaphore, test_join_timeout, test_event,
242 test_sharedvalues ]:
244 print '\n\t######## %s\n' % func.__name__
245 func()
247 ignore = multiprocessing.active_children() # cleanup any old processes
248 if hasattr(multiprocessing, '_debug_info'):
249 info = multiprocessing._debug_info()
250 if info:
251 print info
252 raise ValueError('there should be no positive refcounts left')
255 if __name__ == '__main__':
256 multiprocessing.freeze_support()
258 assert len(sys.argv) in (1, 2)
260 if len(sys.argv) == 1 or sys.argv[1] == 'processes':
261 print ' Using processes '.center(79, '-')
262 namespace = multiprocessing
263 elif sys.argv[1] == 'manager':
264 print ' Using processes and a manager '.center(79, '-')
265 namespace = multiprocessing.Manager()
266 namespace.Process = multiprocessing.Process
267 namespace.current_process = multiprocessing.current_process
268 namespace.active_children = multiprocessing.active_children
269 elif sys.argv[1] == 'threads':
270 print ' Using threads '.center(79, '-')
271 import multiprocessing.dummy as namespace
272 else:
273 print 'Usage:\n\t%s [processes | manager | threads]' % sys.argv[0]
274 raise SystemExit(2)
276 test(namespace)