2008-06-10 23:40:25 -03:00
|
|
|
#
|
|
|
|
# A test file for the `multiprocessing` package
|
|
|
|
#
|
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67398 | benjamin.peterson | 2008-11-26 18:39:17 +0100 (Wed, 26 Nov 2008) | 1 line
fix typo in sqlite3 docs
........
r67423 | jesse.noller | 2008-11-28 19:59:35 +0100 (Fri, 28 Nov 2008) | 2 lines
issue4238: bsd support for cpu_count
........
r67424 | christian.heimes | 2008-11-28 20:33:33 +0100 (Fri, 28 Nov 2008) | 1 line
Retain copyright of processing examples. This was requested by a Debian maintainer during packaging of the multiprocessing package for 2.4/2.5
........
r67432 | benjamin.peterson | 2008-11-29 00:18:46 +0100 (Sat, 29 Nov 2008) | 1 line
SVN format 9 is the same it seems
........
r67440 | jeremy.hylton | 2008-11-29 00:42:59 +0100 (Sat, 29 Nov 2008) | 4 lines
Move definition int sval into branch of ifdef where it is used.
Otherwise, you get a warning about an undefined variable.
........
r67441 | jeremy.hylton | 2008-11-29 01:09:16 +0100 (Sat, 29 Nov 2008) | 2 lines
Reflow long lines.
........
r67444 | amaury.forgeotdarc | 2008-11-29 03:03:32 +0100 (Sat, 29 Nov 2008) | 2 lines
Fix a small typo in docstring
........
r67445 | benjamin.peterson | 2008-11-30 04:07:33 +0100 (Sun, 30 Nov 2008) | 1 line
StringIO.close() stops you from using the buffer, too
........
r67454 | benjamin.peterson | 2008-11-30 15:43:23 +0100 (Sun, 30 Nov 2008) | 1 line
note the version that works
........
r67457 | christian.heimes | 2008-11-30 22:16:28 +0100 (Sun, 30 Nov 2008) | 1 line
w# requires Py_ssize_t
........
r67463 | skip.montanaro | 2008-12-01 02:55:22 +0100 (Mon, 01 Dec 2008) | 1 line
typo in comment
........
2008-12-05 05:00:55 -04:00
|
|
|
# Copyright (c) 2006-2008, R Oudkerk
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
2008-06-10 23:40:25 -03:00
|
|
|
|
|
|
|
import time, sys, random
|
|
|
|
from Queue import Empty
|
|
|
|
|
|
|
|
import multiprocessing # may get overwritten
|
|
|
|
|
|
|
|
|
|
|
|
#### TEST_VALUE
|
|
|
|
|
|
|
|
def value_func(running, mutex):
|
|
|
|
random.seed()
|
|
|
|
time.sleep(random.random()*4)
|
|
|
|
|
|
|
|
mutex.acquire()
|
|
|
|
print '\n\t\t\t' + str(multiprocessing.current_process()) + ' has finished'
|
|
|
|
running.value -= 1
|
|
|
|
mutex.release()
|
|
|
|
|
|
|
|
def test_value():
|
|
|
|
TASKS = 10
|
|
|
|
running = multiprocessing.Value('i', TASKS)
|
|
|
|
mutex = multiprocessing.Lock()
|
|
|
|
|
|
|
|
for i in range(TASKS):
|
|
|
|
p = multiprocessing.Process(target=value_func, args=(running, mutex))
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
while running.value > 0:
|
|
|
|
time.sleep(0.08)
|
|
|
|
mutex.acquire()
|
|
|
|
print running.value,
|
|
|
|
sys.stdout.flush()
|
|
|
|
mutex.release()
|
|
|
|
|
|
|
|
print
|
|
|
|
print 'No more running processes'
|
|
|
|
|
|
|
|
|
|
|
|
#### TEST_QUEUE
|
|
|
|
|
|
|
|
def queue_func(queue):
|
|
|
|
for i in range(30):
|
|
|
|
time.sleep(0.5 * random.random())
|
|
|
|
queue.put(i*i)
|
|
|
|
queue.put('STOP')
|
|
|
|
|
|
|
|
def test_queue():
|
|
|
|
q = multiprocessing.Queue()
|
|
|
|
|
|
|
|
p = multiprocessing.Process(target=queue_func, args=(q,))
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
o = None
|
|
|
|
while o != 'STOP':
|
|
|
|
try:
|
|
|
|
o = q.get(timeout=0.3)
|
|
|
|
print o,
|
|
|
|
sys.stdout.flush()
|
|
|
|
except Empty:
|
|
|
|
print 'TIMEOUT'
|
|
|
|
|
|
|
|
print
|
|
|
|
|
|
|
|
|
|
|
|
#### TEST_CONDITION
|
|
|
|
|
|
|
|
def condition_func(cond):
|
|
|
|
cond.acquire()
|
|
|
|
print '\t' + str(cond)
|
|
|
|
time.sleep(2)
|
|
|
|
print '\tchild is notifying'
|
|
|
|
print '\t' + str(cond)
|
|
|
|
cond.notify()
|
|
|
|
cond.release()
|
|
|
|
|
|
|
|
def test_condition():
|
|
|
|
cond = multiprocessing.Condition()
|
|
|
|
|
|
|
|
p = multiprocessing.Process(target=condition_func, args=(cond,))
|
|
|
|
print cond
|
|
|
|
|
|
|
|
cond.acquire()
|
|
|
|
print cond
|
|
|
|
cond.acquire()
|
|
|
|
print cond
|
|
|
|
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
print 'main is waiting'
|
|
|
|
cond.wait()
|
|
|
|
print 'main has woken up'
|
|
|
|
|
|
|
|
print cond
|
|
|
|
cond.release()
|
|
|
|
print cond
|
|
|
|
cond.release()
|
|
|
|
|
|
|
|
p.join()
|
|
|
|
print cond
|
|
|
|
|
|
|
|
|
|
|
|
#### TEST_SEMAPHORE
|
|
|
|
|
|
|
|
def semaphore_func(sema, mutex, running):
|
|
|
|
sema.acquire()
|
|
|
|
|
|
|
|
mutex.acquire()
|
|
|
|
running.value += 1
|
|
|
|
print running.value, 'tasks are running'
|
|
|
|
mutex.release()
|
|
|
|
|
|
|
|
random.seed()
|
|
|
|
time.sleep(random.random()*2)
|
|
|
|
|
|
|
|
mutex.acquire()
|
|
|
|
running.value -= 1
|
|
|
|
print '%s has finished' % multiprocessing.current_process()
|
|
|
|
mutex.release()
|
|
|
|
|
|
|
|
sema.release()
|
|
|
|
|
|
|
|
def test_semaphore():
|
|
|
|
sema = multiprocessing.Semaphore(3)
|
|
|
|
mutex = multiprocessing.RLock()
|
|
|
|
running = multiprocessing.Value('i', 0)
|
|
|
|
|
|
|
|
processes = [
|
|
|
|
multiprocessing.Process(target=semaphore_func,
|
|
|
|
args=(sema, mutex, running))
|
|
|
|
for i in range(10)
|
|
|
|
]
|
|
|
|
|
|
|
|
for p in processes:
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
for p in processes:
|
|
|
|
p.join()
|
|
|
|
|
|
|
|
|
|
|
|
#### TEST_JOIN_TIMEOUT
|
|
|
|
|
|
|
|
def join_timeout_func():
|
|
|
|
print '\tchild sleeping'
|
|
|
|
time.sleep(5.5)
|
|
|
|
print '\n\tchild terminating'
|
|
|
|
|
|
|
|
def test_join_timeout():
|
|
|
|
p = multiprocessing.Process(target=join_timeout_func)
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
print 'waiting for process to finish'
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
p.join(timeout=1)
|
|
|
|
if not p.is_alive():
|
|
|
|
break
|
|
|
|
print '.',
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
#### TEST_EVENT
|
|
|
|
|
|
|
|
def event_func(event):
|
|
|
|
print '\t%r is waiting' % multiprocessing.current_process()
|
|
|
|
event.wait()
|
|
|
|
print '\t%r has woken up' % multiprocessing.current_process()
|
|
|
|
|
|
|
|
def test_event():
|
|
|
|
event = multiprocessing.Event()
|
|
|
|
|
|
|
|
processes = [multiprocessing.Process(target=event_func, args=(event,))
|
|
|
|
for i in range(5)]
|
|
|
|
|
|
|
|
for p in processes:
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
print 'main is sleeping'
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
print 'main is setting event'
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
for p in processes:
|
|
|
|
p.join()
|
|
|
|
|
|
|
|
|
|
|
|
#### TEST_SHAREDVALUES
|
|
|
|
|
|
|
|
def sharedvalues_func(values, arrays, shared_values, shared_arrays):
|
|
|
|
for i in range(len(values)):
|
|
|
|
v = values[i][1]
|
|
|
|
sv = shared_values[i].value
|
|
|
|
assert v == sv
|
|
|
|
|
|
|
|
for i in range(len(values)):
|
|
|
|
a = arrays[i][1]
|
|
|
|
sa = list(shared_arrays[i][:])
|
|
|
|
assert a == sa
|
|
|
|
|
|
|
|
print 'Tests passed'
|
|
|
|
|
|
|
|
def test_sharedvalues():
|
|
|
|
values = [
|
|
|
|
('i', 10),
|
|
|
|
('h', -2),
|
|
|
|
('d', 1.25)
|
|
|
|
]
|
|
|
|
arrays = [
|
|
|
|
('i', range(100)),
|
|
|
|
('d', [0.25 * i for i in range(100)]),
|
|
|
|
('H', range(1000))
|
|
|
|
]
|
|
|
|
|
|
|
|
shared_values = [multiprocessing.Value(id, v) for id, v in values]
|
|
|
|
shared_arrays = [multiprocessing.Array(id, a) for id, a in arrays]
|
|
|
|
|
|
|
|
p = multiprocessing.Process(
|
|
|
|
target=sharedvalues_func,
|
|
|
|
args=(values, arrays, shared_values, shared_arrays)
|
|
|
|
)
|
|
|
|
p.start()
|
|
|
|
p.join()
|
|
|
|
|
2008-08-19 16:06:19 -03:00
|
|
|
assert p.exitcode == 0
|
2008-06-10 23:40:25 -03:00
|
|
|
|
|
|
|
|
|
|
|
####
|
|
|
|
|
|
|
|
def test(namespace=multiprocessing):
|
|
|
|
global multiprocessing
|
|
|
|
|
|
|
|
multiprocessing = namespace
|
|
|
|
|
|
|
|
for func in [ test_value, test_queue, test_condition,
|
|
|
|
test_semaphore, test_join_timeout, test_event,
|
|
|
|
test_sharedvalues ]:
|
|
|
|
|
|
|
|
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
|
Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72558 | benjamin.peterson | 2009-05-11 01:52:09 +0200 (Mo, 11 Mai 2009) | 1 line
sys.setdefaultencoding() strikes me as a bad example
........
r72745 | benjamin.peterson | 2009-05-17 16:16:29 +0200 (So, 17 Mai 2009) | 1 line
ignore .rst files in sphinx its self
........
r72750 | benjamin.peterson | 2009-05-17 18:59:27 +0200 (So, 17 Mai 2009) | 1 line
chop off slash
........
r72876 | benjamin.peterson | 2009-05-23 22:59:09 +0200 (Sa, 23 Mai 2009) | 1 line
remove mention of old ctypes version
........
r73042 | benjamin.peterson | 2009-05-30 05:10:52 +0200 (Sa, 30 Mai 2009) | 1 line
no fdatasync on macos
........
r73045 | georg.brandl | 2009-05-30 09:26:04 +0200 (Sa, 30 Mai 2009) | 1 line
#6146: fix markup bug.
........
r73046 | georg.brandl | 2009-05-30 09:31:25 +0200 (Sa, 30 Mai 2009) | 1 line
Use preferred form of raising exceptions.
........
r73047 | georg.brandl | 2009-05-30 12:33:23 +0200 (Sa, 30 Mai 2009) | 1 line
Fix some more small markup problems.
........
r73048 | georg.brandl | 2009-05-30 12:34:25 +0200 (Sa, 30 Mai 2009) | 1 line
Fix markup problem.
........
r73069 | benjamin.peterson | 2009-05-31 02:42:42 +0200 (So, 31 Mai 2009) | 1 line
fix signature
........
r73089 | andrew.kuchling | 2009-06-01 02:14:19 +0200 (Mo, 01 Jun 2009) | 1 line
The class for regexes isn't called RegexObject any more; correct the text
........
r73163 | georg.brandl | 2009-06-03 09:25:35 +0200 (Mi, 03 Jun 2009) | 1 line
Use the preferred form of raise statements in the docs.
........
r73186 | georg.brandl | 2009-06-03 23:21:09 +0200 (Mi, 03 Jun 2009) | 1 line
#6174: fix indentation in code example.
........
r73213 | georg.brandl | 2009-06-04 12:15:57 +0200 (Do, 04 Jun 2009) | 1 line
#5967: note that the C slicing APIs do not support negative indices.
........
r73215 | georg.brandl | 2009-06-04 12:22:31 +0200 (Do, 04 Jun 2009) | 1 line
#6176: fix man page section for flock(2).
........
r73217 | georg.brandl | 2009-06-04 12:27:21 +0200 (Do, 04 Jun 2009) | 1 line
#6175: document that inet_aton supports alternate input formats with less than three dots.
........
r73257 | georg.brandl | 2009-06-06 19:50:05 +0200 (Sa, 06 Jun 2009) | 1 line
#6211: elaborate a bit on ways to call the function.
........
r73258 | georg.brandl | 2009-06-06 19:51:31 +0200 (Sa, 06 Jun 2009) | 1 line
#6204: use a real reference instead of "see later".
........
r73260 | georg.brandl | 2009-06-06 20:21:58 +0200 (Sa, 06 Jun 2009) | 1 line
#6224: s/JPython/Jython/, and remove one link to a module nine years old.
........
2009-10-27 11:19:50 -03:00
|
|
|
raise ValueError('there should be no positive refcounts left')
|
2008-06-10 23:40:25 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
multiprocessing.freeze_support()
|
|
|
|
|
|
|
|
assert len(sys.argv) in (1, 2)
|
|
|
|
|
|
|
|
if len(sys.argv) == 1 or sys.argv[1] == 'processes':
|
|
|
|
print ' Using processes '.center(79, '-')
|
|
|
|
namespace = multiprocessing
|
|
|
|
elif sys.argv[1] == 'manager':
|
|
|
|
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, '-')
|
|
|
|
import multiprocessing.dummy as namespace
|
|
|
|
else:
|
|
|
|
print 'Usage:\n\t%s [processes | manager | threads]' % sys.argv[0]
|
Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72558 | benjamin.peterson | 2009-05-11 01:52:09 +0200 (Mo, 11 Mai 2009) | 1 line
sys.setdefaultencoding() strikes me as a bad example
........
r72745 | benjamin.peterson | 2009-05-17 16:16:29 +0200 (So, 17 Mai 2009) | 1 line
ignore .rst files in sphinx its self
........
r72750 | benjamin.peterson | 2009-05-17 18:59:27 +0200 (So, 17 Mai 2009) | 1 line
chop off slash
........
r72876 | benjamin.peterson | 2009-05-23 22:59:09 +0200 (Sa, 23 Mai 2009) | 1 line
remove mention of old ctypes version
........
r73042 | benjamin.peterson | 2009-05-30 05:10:52 +0200 (Sa, 30 Mai 2009) | 1 line
no fdatasync on macos
........
r73045 | georg.brandl | 2009-05-30 09:26:04 +0200 (Sa, 30 Mai 2009) | 1 line
#6146: fix markup bug.
........
r73046 | georg.brandl | 2009-05-30 09:31:25 +0200 (Sa, 30 Mai 2009) | 1 line
Use preferred form of raising exceptions.
........
r73047 | georg.brandl | 2009-05-30 12:33:23 +0200 (Sa, 30 Mai 2009) | 1 line
Fix some more small markup problems.
........
r73048 | georg.brandl | 2009-05-30 12:34:25 +0200 (Sa, 30 Mai 2009) | 1 line
Fix markup problem.
........
r73069 | benjamin.peterson | 2009-05-31 02:42:42 +0200 (So, 31 Mai 2009) | 1 line
fix signature
........
r73089 | andrew.kuchling | 2009-06-01 02:14:19 +0200 (Mo, 01 Jun 2009) | 1 line
The class for regexes isn't called RegexObject any more; correct the text
........
r73163 | georg.brandl | 2009-06-03 09:25:35 +0200 (Mi, 03 Jun 2009) | 1 line
Use the preferred form of raise statements in the docs.
........
r73186 | georg.brandl | 2009-06-03 23:21:09 +0200 (Mi, 03 Jun 2009) | 1 line
#6174: fix indentation in code example.
........
r73213 | georg.brandl | 2009-06-04 12:15:57 +0200 (Do, 04 Jun 2009) | 1 line
#5967: note that the C slicing APIs do not support negative indices.
........
r73215 | georg.brandl | 2009-06-04 12:22:31 +0200 (Do, 04 Jun 2009) | 1 line
#6176: fix man page section for flock(2).
........
r73217 | georg.brandl | 2009-06-04 12:27:21 +0200 (Do, 04 Jun 2009) | 1 line
#6175: document that inet_aton supports alternate input formats with less than three dots.
........
r73257 | georg.brandl | 2009-06-06 19:50:05 +0200 (Sa, 06 Jun 2009) | 1 line
#6211: elaborate a bit on ways to call the function.
........
r73258 | georg.brandl | 2009-06-06 19:51:31 +0200 (Sa, 06 Jun 2009) | 1 line
#6204: use a real reference instead of "see later".
........
r73260 | georg.brandl | 2009-06-06 20:21:58 +0200 (Sa, 06 Jun 2009) | 1 line
#6224: s/JPython/Jython/, and remove one link to a module nine years old.
........
2009-10-27 11:19:50 -03:00
|
|
|
raise SystemExit(2)
|
2008-06-10 23:40:25 -03:00
|
|
|
|
|
|
|
test(namespace)
|