mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
2to3 run of multiprocessing examples.
mp_benchmarks, mp_newtypes and mp_distribution are still broken but the others are working properly. We should include the examples in our unit test suite ...
This commit is contained in:
parent
94e0772989
commit
aae1b70a83
6 changed files with 177 additions and 177 deletions
|
|
@ -3,7 +3,7 @@
|
|||
#
|
||||
|
||||
import time, sys, random
|
||||
from Queue import Empty
|
||||
from queue import Empty
|
||||
|
||||
import multiprocessing # may get overwritten
|
||||
|
||||
|
|
@ -15,7 +15,7 @@ def value_func(running, mutex):
|
|||
time.sleep(random.random()*4)
|
||||
|
||||
mutex.acquire()
|
||||
print '\n\t\t\t' + str(multiprocessing.current_process()) + ' has finished'
|
||||
print('\n\t\t\t' + str(multiprocessing.current_process()) + ' has finished')
|
||||
running.value -= 1
|
||||
mutex.release()
|
||||
|
||||
|
|
@ -31,12 +31,12 @@ def test_value():
|
|||
while running.value > 0:
|
||||
time.sleep(0.08)
|
||||
mutex.acquire()
|
||||
print running.value,
|
||||
print(running.value, end=' ')
|
||||
sys.stdout.flush()
|
||||
mutex.release()
|
||||
|
||||
print
|
||||
print 'No more running processes'
|
||||
print()
|
||||
print('No more running processes')
|
||||
|
||||
|
||||
#### TEST_QUEUE
|
||||
|
|
@ -57,22 +57,22 @@ def test_queue():
|
|||
while o != 'STOP':
|
||||
try:
|
||||
o = q.get(timeout=0.3)
|
||||
print o,
|
||||
print(o, end=' ')
|
||||
sys.stdout.flush()
|
||||
except Empty:
|
||||
print 'TIMEOUT'
|
||||
print('TIMEOUT')
|
||||
|
||||
print
|
||||
print()
|
||||
|
||||
|
||||
#### TEST_CONDITION
|
||||
|
||||
def condition_func(cond):
|
||||
cond.acquire()
|
||||
print '\t' + str(cond)
|
||||
print('\t' + str(cond))
|
||||
time.sleep(2)
|
||||
print '\tchild is notifying'
|
||||
print '\t' + str(cond)
|
||||
print('\tchild is notifying')
|
||||
print('\t' + str(cond))
|
||||
cond.notify()
|
||||
cond.release()
|
||||
|
||||
|
|
@ -80,26 +80,26 @@ def test_condition():
|
|||
cond = multiprocessing.Condition()
|
||||
|
||||
p = multiprocessing.Process(target=condition_func, args=(cond,))
|
||||
print cond
|
||||
print(cond)
|
||||
|
||||
cond.acquire()
|
||||
print cond
|
||||
print(cond)
|
||||
cond.acquire()
|
||||
print cond
|
||||
print(cond)
|
||||
|
||||
p.start()
|
||||
|
||||
print 'main is waiting'
|
||||
print('main is waiting')
|
||||
cond.wait()
|
||||
print 'main has woken up'
|
||||
print('main has woken up')
|
||||
|
||||
print cond
|
||||
print(cond)
|
||||
cond.release()
|
||||
print cond
|
||||
print(cond)
|
||||
cond.release()
|
||||
|
||||
p.join()
|
||||
print cond
|
||||
print(cond)
|
||||
|
||||
|
||||
#### TEST_SEMAPHORE
|
||||
|
|
@ -109,7 +109,7 @@ def semaphore_func(sema, mutex, running):
|
|||
|
||||
mutex.acquire()
|
||||
running.value += 1
|
||||
print running.value, 'tasks are running'
|
||||
print(running.value, 'tasks are running')
|
||||
mutex.release()
|
||||
|
||||
random.seed()
|
||||
|
|
@ -117,7 +117,7 @@ def semaphore_func(sema, mutex, running):
|
|||
|
||||
mutex.acquire()
|
||||
running.value -= 1
|
||||
print '%s has finished' % multiprocessing.current_process()
|
||||
print('%s has finished' % multiprocessing.current_process())
|
||||
mutex.release()
|
||||
|
||||
sema.release()
|
||||
|
|
@ -143,30 +143,30 @@ def test_semaphore():
|
|||
#### TEST_JOIN_TIMEOUT
|
||||
|
||||
def join_timeout_func():
|
||||
print '\tchild sleeping'
|
||||
print('\tchild sleeping')
|
||||
time.sleep(5.5)
|
||||
print '\n\tchild terminating'
|
||||
print('\n\tchild terminating')
|
||||
|
||||
def test_join_timeout():
|
||||
p = multiprocessing.Process(target=join_timeout_func)
|
||||
p.start()
|
||||
|
||||
print 'waiting for process to finish'
|
||||
print('waiting for process to finish')
|
||||
|
||||
while 1:
|
||||
p.join(timeout=1)
|
||||
if not p.is_alive():
|
||||
break
|
||||
print '.',
|
||||
print('.', end=' ')
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
#### TEST_EVENT
|
||||
|
||||
def event_func(event):
|
||||
print '\t%r is waiting' % multiprocessing.current_process()
|
||||
print('\t%r is waiting' % multiprocessing.current_process())
|
||||
event.wait()
|
||||
print '\t%r has woken up' % multiprocessing.current_process()
|
||||
print('\t%r has woken up' % multiprocessing.current_process())
|
||||
|
||||
def test_event():
|
||||
event = multiprocessing.Event()
|
||||
|
|
@ -177,10 +177,10 @@ def test_event():
|
|||
for p in processes:
|
||||
p.start()
|
||||
|
||||
print 'main is sleeping'
|
||||
print('main is sleeping')
|
||||
time.sleep(2)
|
||||
|
||||
print 'main is setting event'
|
||||
print('main is setting event')
|
||||
event.set()
|
||||
|
||||
for p in processes:
|
||||
|
|
@ -200,7 +200,7 @@ def sharedvalues_func(values, arrays, shared_values, shared_arrays):
|
|||
sa = list(shared_arrays[i][:])
|
||||
assert a == sa
|
||||
|
||||
print 'Tests passed'
|
||||
print('Tests passed')
|
||||
|
||||
def test_sharedvalues():
|
||||
values = [
|
||||
|
|
@ -209,9 +209,9 @@ def test_sharedvalues():
|
|||
('d', 1.25)
|
||||
]
|
||||
arrays = [
|
||||
('i', range(100)),
|
||||
('i', list(range(100))),
|
||||
('d', [0.25 * i for i in range(100)]),
|
||||
('H', range(1000))
|
||||
('H', list(range(1000)))
|
||||
]
|
||||
|
||||
shared_values = [multiprocessing.Value(id, v) for id, v in values]
|
||||
|
|
@ -238,15 +238,15 @@ def test(namespace=multiprocessing):
|
|||
test_semaphore, test_join_timeout, test_event,
|
||||
test_sharedvalues ]:
|
||||
|
||||
print '\n\t######## %s\n' % func.__name__
|
||||
print('\n\t######## %s\n' % func.__name__)
|
||||
func()
|
||||
|
||||
ignore = multiprocessing.active_children() # cleanup any old processes
|
||||
if hasattr(multiprocessing, '_debug_info'):
|
||||
info = multiprocessing._debug_info()
|
||||
if info:
|
||||
print info
|
||||
raise ValueError, 'there should be no positive refcounts left'
|
||||
print(info)
|
||||
raise ValueError('there should be no positive refcounts left')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
@ -255,19 +255,19 @@ if __name__ == '__main__':
|
|||
assert len(sys.argv) in (1, 2)
|
||||
|
||||
if len(sys.argv) == 1 or sys.argv[1] == 'processes':
|
||||
print ' Using processes '.center(79, '-')
|
||||
print(' Using processes '.center(79, '-'))
|
||||
namespace = multiprocessing
|
||||
elif sys.argv[1] == 'manager':
|
||||
print ' Using processes and a manager '.center(79, '-')
|
||||
print(' Using processes and a manager '.center(79, '-'))
|
||||
namespace = multiprocessing.Manager()
|
||||
namespace.Process = multiprocessing.Process
|
||||
namespace.current_process = multiprocessing.current_process
|
||||
namespace.active_children = multiprocessing.active_children
|
||||
elif sys.argv[1] == 'threads':
|
||||
print ' Using threads '.center(79, '-')
|
||||
print(' Using threads '.center(79, '-'))
|
||||
import multiprocessing.dummy as namespace
|
||||
else:
|
||||
print 'Usage:\n\t%s [processes | manager | threads]' % sys.argv[0]
|
||||
raise SystemExit, 2
|
||||
print('Usage:\n\t%s [processes | manager | threads]' % sys.argv[0])
|
||||
raise SystemExit(2)
|
||||
|
||||
test(namespace)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue