annotate Lib/test/test_multiprocessing.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 7e319eefab8c
children 70adf0f68fbc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47876
309542fd7fe8 Add #!/usr/bin/env python for ben
Jesse Noller <jnoller@gmail.com>
parents: 47874
diff changeset
1 #!/usr/bin/env python
309542fd7fe8 Add #!/usr/bin/env python for ben
Jesse Noller <jnoller@gmail.com>
parents: 47874
diff changeset
2
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
3 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
4 # Unit tests for the multiprocessing package
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
5 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
6
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
7 import unittest
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
8 import threading
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
9 import Queue
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
10 import time
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
11 import sys
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
12 import os
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
13 import gc
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
14 import signal
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
15 import array
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
16 import copy
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
17 import socket
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
18 import random
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
19 import logging
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 52477
diff changeset
20 import test_support
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
21
49346
b4964df64384 issue3770: if SEM_OPEN is 0, disable the mp.synchronize module, rev. Nick Coghlan, Damien Miller
Jesse Noller <jnoller@gmail.com>
parents: 48866
diff changeset
22
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 52477
diff changeset
23 _multiprocessing = test_support.import_module('_multiprocessing')
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 52477
diff changeset
24
49346
b4964df64384 issue3770: if SEM_OPEN is 0, disable the mp.synchronize module, rev. Nick Coghlan, Damien Miller
Jesse Noller <jnoller@gmail.com>
parents: 48866
diff changeset
25 # Work around broken sem_open implementations
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 52477
diff changeset
26 test_support.import_module('multiprocessing.synchronize')
49346
b4964df64384 issue3770: if SEM_OPEN is 0, disable the mp.synchronize module, rev. Nick Coghlan, Damien Miller
Jesse Noller <jnoller@gmail.com>
parents: 48866
diff changeset
27
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
28 import multiprocessing.dummy
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
29 import multiprocessing.connection
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
30 import multiprocessing.managers
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
31 import multiprocessing.heap
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
32 import multiprocessing.pool
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
33
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
34 from multiprocessing import util
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
35
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
36 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
37 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
38 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
39
47946
041550738201 remove sys.version_info check for 3.0
Benjamin Peterson <benjamin@python.org>
parents: 47945
diff changeset
40 latin = str
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
41
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
42 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
43 # Constants
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
44 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
45
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
46 LOG_LEVEL = util.SUBWARNING
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
47 #LOG_LEVEL = logging.WARNING
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
48
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
49 DELTA = 0.1
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
50 CHECK_TIMINGS = False # making true makes tests take a lot longer
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
51 # and can sometimes cause some non-serious
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
52 # failures because some calls block a bit
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
53 # longer than expected
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
54 if CHECK_TIMINGS:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
55 TIMEOUT1, TIMEOUT2, TIMEOUT3 = 0.82, 0.35, 1.4
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
56 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
57 TIMEOUT1, TIMEOUT2, TIMEOUT3 = 0.1, 0.1, 0.1
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
58
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
59 HAVE_GETVALUE = not getattr(_multiprocessing,
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
60 'HAVE_BROKEN_SEM_GETVALUE', False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
61
52445
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
62 WIN32 = (sys.platform == "win32")
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
63
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
64 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
65 # Creates a wrapper for a function which records the time it takes to finish
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
66 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
67
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
68 class TimingWrapper(object):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
69
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
70 def __init__(self, func):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
71 self.func = func
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
72 self.elapsed = None
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
73
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
74 def __call__(self, *args, **kwds):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
75 t = time.time()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
76 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
77 return self.func(*args, **kwds)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
78 finally:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
79 self.elapsed = time.time() - t
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
80
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
81 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
82 # Base class for test cases
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
83 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
84
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
85 class BaseTestCase(object):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
86
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
87 ALLOWED_TYPES = ('processes', 'manager', 'threads')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
88
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
89 def assertTimingAlmostEqual(self, a, b):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
90 if CHECK_TIMINGS:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
91 self.assertAlmostEqual(a, b, 1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
92
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
93 def assertReturnsIfImplemented(self, value, func, *args):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
94 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
95 res = func(*args)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
96 except NotImplementedError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
97 pass
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
98 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
99 return self.assertEqual(value, res)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
100
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
101 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
102 # Return the value of a semaphore
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
103 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
104
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
105 def get_value(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
106 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
107 return self.get_value()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
108 except AttributeError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
109 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
110 return self._Semaphore__value
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
111 except AttributeError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
112 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
113 return self._value
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
114 except AttributeError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
115 raise NotImplementedError
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
116
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
117 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
118 # Testcases
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
119 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
120
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
121 class _TestProcess(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
122
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
123 ALLOWED_TYPES = ('processes', 'threads')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
124
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
125 def test_current(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
126 if self.TYPE == 'threads':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
127 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
128
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
129 current = self.current_process()
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
130 authkey = current.authkey
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
131
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
132 self.assertTrue(current.is_alive())
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
133 self.assertTrue(not current.daemon)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
134 self.assertTrue(isinstance(authkey, bytes))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
135 self.assertTrue(len(authkey) > 0)
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
136 self.assertEqual(current.ident, os.getpid())
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
137 self.assertEqual(current.exitcode, None)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
138
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
139 def _test(self, q, *args, **kwds):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
140 current = self.current_process()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
141 q.put(args)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
142 q.put(kwds)
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
143 q.put(current.name)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
144 if self.TYPE != 'threads':
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
145 q.put(bytes(current.authkey))
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
146 q.put(current.pid)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
147
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
148 def test_process(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
149 q = self.Queue(1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
150 e = self.Event()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
151 args = (q, 1, 2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
152 kwargs = {'hello':23, 'bye':2.54}
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
153 name = 'SomeProcess'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
154 p = self.Process(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
155 target=self._test, args=args, kwargs=kwargs, name=name
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
156 )
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
157 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
158 current = self.current_process()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
159
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
160 if self.TYPE != 'threads':
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
161 self.assertEquals(p.authkey, current.authkey)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
162 self.assertEquals(p.is_alive(), False)
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
163 self.assertEquals(p.daemon, True)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
164 self.assertTrue(p not in self.active_children())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
165 self.assertTrue(type(self.active_children()) is list)
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
166 self.assertEqual(p.exitcode, None)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
167
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
168 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
169
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
170 self.assertEquals(p.exitcode, None)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
171 self.assertEquals(p.is_alive(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
172 self.assertTrue(p in self.active_children())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
173
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
174 self.assertEquals(q.get(), args[1:])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
175 self.assertEquals(q.get(), kwargs)
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
176 self.assertEquals(q.get(), p.name)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
177 if self.TYPE != 'threads':
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
178 self.assertEquals(q.get(), current.authkey)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
179 self.assertEquals(q.get(), p.pid)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
180
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
181 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
182
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
183 self.assertEquals(p.exitcode, 0)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
184 self.assertEquals(p.is_alive(), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
185 self.assertTrue(p not in self.active_children())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
186
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
187 def _test_terminate(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
188 time.sleep(1000)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
189
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
190 def test_terminate(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
191 if self.TYPE == 'threads':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
192 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
193
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
194 p = self.Process(target=self._test_terminate)
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
195 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
196 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
197
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
198 self.assertEqual(p.is_alive(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
199 self.assertTrue(p in self.active_children())
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
200 self.assertEqual(p.exitcode, None)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
201
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
202 p.terminate()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
203
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
204 join = TimingWrapper(p.join)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
205 self.assertEqual(join(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
206 self.assertTimingAlmostEqual(join.elapsed, 0.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
207
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
208 self.assertEqual(p.is_alive(), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
209 self.assertTrue(p not in self.active_children())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
210
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
211 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
212
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
213 # XXX sometimes get p.exitcode == 0 on Windows ...
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
214 #self.assertEqual(p.exitcode, -signal.SIGTERM)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
215
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
216 def test_cpu_count(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
217 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
218 cpus = multiprocessing.cpu_count()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
219 except NotImplementedError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
220 cpus = 1
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
221 self.assertTrue(type(cpus) is int)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
222 self.assertTrue(cpus >= 1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
223
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
224 def test_active_children(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
225 self.assertEqual(type(self.active_children()), list)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
226
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
227 p = self.Process(target=time.sleep, args=(DELTA,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
228 self.assertTrue(p not in self.active_children())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
229
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
230 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
231 self.assertTrue(p in self.active_children())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
232
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
233 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
234 self.assertTrue(p not in self.active_children())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
235
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
236 def _test_recursion(self, wconn, id):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
237 from multiprocessing import forking
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
238 wconn.send(id)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
239 if len(id) < 2:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
240 for i in range(2):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
241 p = self.Process(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
242 target=self._test_recursion, args=(wconn, id+[i])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
243 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
244 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
245 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
246
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
247 def test_recursion(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
248 rconn, wconn = self.Pipe(duplex=False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
249 self._test_recursion(wconn, [])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
250
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
251 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
252 result = []
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
253 while rconn.poll():
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
254 result.append(rconn.recv())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
255
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
256 expected = [
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
257 [],
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
258 [0],
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
259 [0, 0],
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
260 [0, 1],
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
261 [1],
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
262 [1, 0],
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
263 [1, 1]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
264 ]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
265 self.assertEqual(result, expected)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
266
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
267 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
268 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
269 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
270
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
271 class _UpperCaser(multiprocessing.Process):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
272
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
273 def __init__(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
274 multiprocessing.Process.__init__(self)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
275 self.child_conn, self.parent_conn = multiprocessing.Pipe()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
276
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
277 def run(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
278 self.parent_conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
279 for s in iter(self.child_conn.recv, None):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
280 self.child_conn.send(s.upper())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
281 self.child_conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
282
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
283 def submit(self, s):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
284 assert type(s) is str
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
285 self.parent_conn.send(s)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
286 return self.parent_conn.recv()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
287
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
288 def stop(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
289 self.parent_conn.send(None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
290 self.parent_conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
291 self.child_conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
292
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
293 class _TestSubclassingProcess(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
294
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
295 ALLOWED_TYPES = ('processes',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
296
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
297 def test_subclassing(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
298 uppercaser = _UpperCaser()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
299 uppercaser.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
300 self.assertEqual(uppercaser.submit('hello'), 'HELLO')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
301 self.assertEqual(uppercaser.submit('world'), 'WORLD')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
302 uppercaser.stop()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
303 uppercaser.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
304
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
305 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
306 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
307 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
308
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
309 def queue_empty(q):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
310 if hasattr(q, 'empty'):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
311 return q.empty()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
312 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
313 return q.qsize() == 0
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
314
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
315 def queue_full(q, maxsize):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
316 if hasattr(q, 'full'):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
317 return q.full()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
318 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
319 return q.qsize() == maxsize
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
320
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
321
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
322 class _TestQueue(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
323
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
324
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
325 def _test_put(self, queue, child_can_start, parent_can_continue):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
326 child_can_start.wait()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
327 for i in range(6):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
328 queue.get()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
329 parent_can_continue.set()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
330
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
331 def test_put(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
332 MAXSIZE = 6
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
333 queue = self.Queue(maxsize=MAXSIZE)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
334 child_can_start = self.Event()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
335 parent_can_continue = self.Event()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
336
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
337 proc = self.Process(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
338 target=self._test_put,
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
339 args=(queue, child_can_start, parent_can_continue)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
340 )
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
341 proc.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
342 proc.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
343
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
344 self.assertEqual(queue_empty(queue), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
345 self.assertEqual(queue_full(queue, MAXSIZE), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
346
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
347 queue.put(1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
348 queue.put(2, True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
349 queue.put(3, True, None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
350 queue.put(4, False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
351 queue.put(5, False, None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
352 queue.put_nowait(6)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
353
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
354 # the values may be in buffer but not yet in pipe so sleep a bit
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
355 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
356
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
357 self.assertEqual(queue_empty(queue), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
358 self.assertEqual(queue_full(queue, MAXSIZE), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
359
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
360 put = TimingWrapper(queue.put)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
361 put_nowait = TimingWrapper(queue.put_nowait)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
362
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
363 self.assertRaises(Queue.Full, put, 7, False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
364 self.assertTimingAlmostEqual(put.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
365
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
366 self.assertRaises(Queue.Full, put, 7, False, None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
367 self.assertTimingAlmostEqual(put.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
368
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
369 self.assertRaises(Queue.Full, put_nowait, 7)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
370 self.assertTimingAlmostEqual(put_nowait.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
371
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
372 self.assertRaises(Queue.Full, put, 7, True, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
373 self.assertTimingAlmostEqual(put.elapsed, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
374
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
375 self.assertRaises(Queue.Full, put, 7, False, TIMEOUT2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
376 self.assertTimingAlmostEqual(put.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
377
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
378 self.assertRaises(Queue.Full, put, 7, True, timeout=TIMEOUT3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
379 self.assertTimingAlmostEqual(put.elapsed, TIMEOUT3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
380
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
381 child_can_start.set()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
382 parent_can_continue.wait()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
383
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
384 self.assertEqual(queue_empty(queue), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
385 self.assertEqual(queue_full(queue, MAXSIZE), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
386
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
387 proc.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
388
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
389 def _test_get(self, queue, child_can_start, parent_can_continue):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
390 child_can_start.wait()
47739
f8a8894da77a reduce the test_multiprocessing load to ones that shouldn't hang. These will be reenabled gradually as we find the problems.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
391 #queue.put(1)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
392 queue.put(2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
393 queue.put(3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
394 queue.put(4)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
395 queue.put(5)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
396 parent_can_continue.set()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
397
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
398 def test_get(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
399 queue = self.Queue()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
400 child_can_start = self.Event()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
401 parent_can_continue = self.Event()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
402
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
403 proc = self.Process(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
404 target=self._test_get,
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
405 args=(queue, child_can_start, parent_can_continue)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
406 )
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
407 proc.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
408 proc.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
409
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
410 self.assertEqual(queue_empty(queue), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
411
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
412 child_can_start.set()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
413 parent_can_continue.wait()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
414
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
415 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
416 self.assertEqual(queue_empty(queue), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
417
47739
f8a8894da77a reduce the test_multiprocessing load to ones that shouldn't hang. These will be reenabled gradually as we find the problems.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
418 # Hangs unexpectedly, remove for now
f8a8894da77a reduce the test_multiprocessing load to ones that shouldn't hang. These will be reenabled gradually as we find the problems.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
419 #self.assertEqual(queue.get(), 1)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
420 self.assertEqual(queue.get(True, None), 2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
421 self.assertEqual(queue.get(True), 3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
422 self.assertEqual(queue.get(timeout=1), 4)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
423 self.assertEqual(queue.get_nowait(), 5)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
424
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
425 self.assertEqual(queue_empty(queue), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
426
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
427 get = TimingWrapper(queue.get)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
428 get_nowait = TimingWrapper(queue.get_nowait)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
429
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
430 self.assertRaises(Queue.Empty, get, False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
431 self.assertTimingAlmostEqual(get.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
432
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
433 self.assertRaises(Queue.Empty, get, False, None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
434 self.assertTimingAlmostEqual(get.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
435
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
436 self.assertRaises(Queue.Empty, get_nowait)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
437 self.assertTimingAlmostEqual(get_nowait.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
438
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
439 self.assertRaises(Queue.Empty, get, True, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
440 self.assertTimingAlmostEqual(get.elapsed, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
441
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
442 self.assertRaises(Queue.Empty, get, False, TIMEOUT2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
443 self.assertTimingAlmostEqual(get.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
444
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
445 self.assertRaises(Queue.Empty, get, timeout=TIMEOUT3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
446 self.assertTimingAlmostEqual(get.elapsed, TIMEOUT3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
447
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
448 proc.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
449
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
450 def _test_fork(self, queue):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
451 for i in range(10, 20):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
452 queue.put(i)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
453 # note that at this point the items may only be buffered, so the
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
454 # process cannot shutdown until the feeder thread has finished
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
455 # pushing items onto the pipe.
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
456
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
457 def test_fork(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
458 # Old versions of Queue would fail to create a new feeder
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
459 # thread for a forked process if the original process had its
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
460 # own feeder thread. This test checks that this no longer
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
461 # happens.
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
462
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
463 queue = self.Queue()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
464
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
465 # put items on queue so that main process starts a feeder thread
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
466 for i in range(10):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
467 queue.put(i)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
468
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
469 # wait to make sure thread starts before we fork a new process
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
470 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
471
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
472 # fork process
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
473 p = self.Process(target=self._test_fork, args=(queue,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
474 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
475
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
476 # check that all expected items are in the queue
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
477 for i in range(20):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
478 self.assertEqual(queue.get(), i)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
479 self.assertRaises(Queue.Empty, queue.get, False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
480
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
481 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
482
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
483 def test_qsize(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
484 q = self.Queue()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
485 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
486 self.assertEqual(q.qsize(), 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
487 except NotImplementedError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
488 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
489 q.put(1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
490 self.assertEqual(q.qsize(), 1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
491 q.put(5)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
492 self.assertEqual(q.qsize(), 2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
493 q.get()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
494 self.assertEqual(q.qsize(), 1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
495 q.get()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
496 self.assertEqual(q.qsize(), 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
497
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
498 def _test_task_done(self, q):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
499 for obj in iter(q.get, None):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
500 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
501 q.task_done()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
502
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
503 def test_task_done(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
504 queue = self.JoinableQueue()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
505
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
506 if sys.version_info < (2, 5) and not hasattr(queue, 'task_done'):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
507 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
508
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
509 workers = [self.Process(target=self._test_task_done, args=(queue,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
510 for i in xrange(4)]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
511
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
512 for p in workers:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
513 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
514
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
515 for i in xrange(10):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
516 queue.put(i)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
517
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
518 queue.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
519
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
520 for p in workers:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
521 queue.put(None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
522
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
523 for p in workers:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
524 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
525
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
526 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
527 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
528 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
529
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
530 class _TestLock(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
531
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
532 def test_lock(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
533 lock = self.Lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
534 self.assertEqual(lock.acquire(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
535 self.assertEqual(lock.acquire(False), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
536 self.assertEqual(lock.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
537 self.assertRaises((ValueError, threading.ThreadError), lock.release)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
538
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
539 def test_rlock(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
540 lock = self.RLock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
541 self.assertEqual(lock.acquire(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
542 self.assertEqual(lock.acquire(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
543 self.assertEqual(lock.acquire(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
544 self.assertEqual(lock.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
545 self.assertEqual(lock.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
546 self.assertEqual(lock.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
547 self.assertRaises((AssertionError, RuntimeError), lock.release)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
548
52498
ea4c5c3bc7ab merge in patch from tim golden to fix contextmanager support for mp.Lock()
Jesse Noller <jnoller@gmail.com>
parents: 52491
diff changeset
549 def test_lock_context(self):
ea4c5c3bc7ab merge in patch from tim golden to fix contextmanager support for mp.Lock()
Jesse Noller <jnoller@gmail.com>
parents: 52491
diff changeset
550 with self.Lock():
ea4c5c3bc7ab merge in patch from tim golden to fix contextmanager support for mp.Lock()
Jesse Noller <jnoller@gmail.com>
parents: 52491
diff changeset
551 pass
ea4c5c3bc7ab merge in patch from tim golden to fix contextmanager support for mp.Lock()
Jesse Noller <jnoller@gmail.com>
parents: 52491
diff changeset
552
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
553
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
554 class _TestSemaphore(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
555
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
556 def _test_semaphore(self, sem):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
557 self.assertReturnsIfImplemented(2, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
558 self.assertEqual(sem.acquire(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
559 self.assertReturnsIfImplemented(1, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
560 self.assertEqual(sem.acquire(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
561 self.assertReturnsIfImplemented(0, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
562 self.assertEqual(sem.acquire(False), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
563 self.assertReturnsIfImplemented(0, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
564 self.assertEqual(sem.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
565 self.assertReturnsIfImplemented(1, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
566 self.assertEqual(sem.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
567 self.assertReturnsIfImplemented(2, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
568
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
569 def test_semaphore(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
570 sem = self.Semaphore(2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
571 self._test_semaphore(sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
572 self.assertEqual(sem.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
573 self.assertReturnsIfImplemented(3, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
574 self.assertEqual(sem.release(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
575 self.assertReturnsIfImplemented(4, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
576
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
577 def test_bounded_semaphore(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
578 sem = self.BoundedSemaphore(2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
579 self._test_semaphore(sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
580 # Currently fails on OS/X
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
581 #if HAVE_GETVALUE:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
582 # self.assertRaises(ValueError, sem.release)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
583 # self.assertReturnsIfImplemented(2, get_value, sem)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
584
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
585 def test_timeout(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
586 if self.TYPE != 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
587 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
588
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
589 sem = self.Semaphore(0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
590 acquire = TimingWrapper(sem.acquire)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
591
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
592 self.assertEqual(acquire(False), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
593 self.assertTimingAlmostEqual(acquire.elapsed, 0.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
594
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
595 self.assertEqual(acquire(False, None), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
596 self.assertTimingAlmostEqual(acquire.elapsed, 0.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
597
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
598 self.assertEqual(acquire(False, TIMEOUT1), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
599 self.assertTimingAlmostEqual(acquire.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
600
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
601 self.assertEqual(acquire(True, TIMEOUT2), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
602 self.assertTimingAlmostEqual(acquire.elapsed, TIMEOUT2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
603
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
604 self.assertEqual(acquire(timeout=TIMEOUT3), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
605 self.assertTimingAlmostEqual(acquire.elapsed, TIMEOUT3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
606
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
607
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
608 class _TestCondition(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
609
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
610 def f(self, cond, sleeping, woken, timeout=None):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
611 cond.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
612 sleeping.release()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
613 cond.wait(timeout)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
614 woken.release()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
615 cond.release()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
616
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
617 def check_invariant(self, cond):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
618 # this is only supposed to succeed when there are no sleepers
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
619 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
620 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
621 sleepers = (cond._sleeping_count.get_value() -
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
622 cond._woken_count.get_value())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
623 self.assertEqual(sleepers, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
624 self.assertEqual(cond._wait_semaphore.get_value(), 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
625 except NotImplementedError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
626 pass
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
627
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
628 def test_notify(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
629 cond = self.Condition()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
630 sleeping = self.Semaphore(0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
631 woken = self.Semaphore(0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
632
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
633 p = self.Process(target=self.f, args=(cond, sleeping, woken))
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
634 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
635 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
636
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
637 p = threading.Thread(target=self.f, args=(cond, sleeping, woken))
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
638 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
639 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
640
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
641 # wait for both children to start sleeping
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
642 sleeping.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
643 sleeping.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
644
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
645 # check no process/thread has woken up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
646 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
647 self.assertReturnsIfImplemented(0, get_value, woken)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
648
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
649 # wake up one process/thread
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
650 cond.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
651 cond.notify()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
652 cond.release()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
653
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
654 # check one process/thread has woken up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
655 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
656 self.assertReturnsIfImplemented(1, get_value, woken)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
657
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
658 # wake up another
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
659 cond.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
660 cond.notify()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
661 cond.release()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
662
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
663 # check other has woken up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
664 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
665 self.assertReturnsIfImplemented(2, get_value, woken)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
666
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
667 # check state is not mucked up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
668 self.check_invariant(cond)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
669 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
670
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
671 def test_notify_all(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
672 cond = self.Condition()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
673 sleeping = self.Semaphore(0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
674 woken = self.Semaphore(0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
675
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
676 # start some threads/processes which will timeout
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
677 for i in range(3):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
678 p = self.Process(target=self.f,
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
679 args=(cond, sleeping, woken, TIMEOUT1))
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
680 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
681 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
682
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
683 t = threading.Thread(target=self.f,
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
684 args=(cond, sleeping, woken, TIMEOUT1))
48289
024b2b984866 change a few uses of the threading APIs
Benjamin Peterson <benjamin@python.org>
parents: 48188
diff changeset
685 t.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
686 t.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
687
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
688 # wait for them all to sleep
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
689 for i in xrange(6):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
690 sleeping.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
691
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
692 # check they have all timed out
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
693 for i in xrange(6):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
694 woken.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
695 self.assertReturnsIfImplemented(0, get_value, woken)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
696
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
697 # check state is not mucked up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
698 self.check_invariant(cond)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
699
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
700 # start some more threads/processes
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
701 for i in range(3):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
702 p = self.Process(target=self.f, args=(cond, sleeping, woken))
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
703 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
704 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
705
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
706 t = threading.Thread(target=self.f, args=(cond, sleeping, woken))
48289
024b2b984866 change a few uses of the threading APIs
Benjamin Peterson <benjamin@python.org>
parents: 48188
diff changeset
707 t.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
708 t.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
709
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
710 # wait for them to all sleep
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
711 for i in xrange(6):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
712 sleeping.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
713
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
714 # check no process/thread has woken up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
715 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
716 self.assertReturnsIfImplemented(0, get_value, woken)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
717
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
718 # wake them all up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
719 cond.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
720 cond.notify_all()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
721 cond.release()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
722
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
723 # check they have all woken
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
724 time.sleep(DELTA)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
725 self.assertReturnsIfImplemented(6, get_value, woken)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
726
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
727 # check state is not mucked up
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
728 self.check_invariant(cond)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
729
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
730 def test_timeout(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
731 cond = self.Condition()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
732 wait = TimingWrapper(cond.wait)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
733 cond.acquire()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
734 res = wait(TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
735 cond.release()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
736 self.assertEqual(res, None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
737 self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
738
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
739
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
740 class _TestEvent(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
741
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
742 def _test_event(self, event):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
743 time.sleep(TIMEOUT2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
744 event.set()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
745
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
746 def test_event(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
747 event = self.Event()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
748 wait = TimingWrapper(event.wait)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
749
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
750 # Removed temporaily, due to API shear, this does not
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
751 # work with threading._Event objects. is_set == isSet
52635
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
752 self.assertEqual(event.is_set(), False)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
753
52635
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
754 # Removed, threading.Event.wait() will return the value of the __flag
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
755 # instead of None. API Shear with the semaphore backed mp.Event
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
756 self.assertEqual(wait(0.0), False)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
757 self.assertTimingAlmostEqual(wait.elapsed, 0.0)
52635
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
758 self.assertEqual(wait(TIMEOUT1), False)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
759 self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
760
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
761 event.set()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
762
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
763 # See note above on the API differences
52635
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
764 self.assertEqual(event.is_set(), True)
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
765 self.assertEqual(wait(), True)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
766 self.assertTimingAlmostEqual(wait.elapsed, 0.0)
52635
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
767 self.assertEqual(wait(TIMEOUT1), True)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
768 self.assertTimingAlmostEqual(wait.elapsed, 0.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
769 # self.assertEqual(event.is_set(), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
770
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
771 event.clear()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
772
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
773 #self.assertEqual(event.is_set(), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
774
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
775 self.Process(target=self._test_event, args=(event,)).start()
52635
cc2c7133da4f Fix multiprocessing.event to match the new threading.Event API
Jesse Noller <jnoller@gmail.com>
parents: 52498
diff changeset
776 self.assertEqual(wait(), True)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
777
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
778 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
779 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
780 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
781
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
782 class _TestValue(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
783
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
784 codes_values = [
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
785 ('i', 4343, 24234),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
786 ('d', 3.625, -4.25),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
787 ('h', -232, 234),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
788 ('c', latin('x'), latin('y'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
789 ]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
790
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
791 def _test(self, values):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
792 for sv, cv in zip(values, self.codes_values):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
793 sv.value = cv[2]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
794
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
795
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
796 def test_value(self, raw=False):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
797 if self.TYPE != 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
798 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
799
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
800 if raw:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
801 values = [self.RawValue(code, value)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
802 for code, value, _ in self.codes_values]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
803 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
804 values = [self.Value(code, value)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
805 for code, value, _ in self.codes_values]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
806
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
807 for sv, cv in zip(values, self.codes_values):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
808 self.assertEqual(sv.value, cv[1])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
809
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
810 proc = self.Process(target=self._test, args=(values,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
811 proc.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
812 proc.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
813
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
814 for sv, cv in zip(values, self.codes_values):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
815 self.assertEqual(sv.value, cv[2])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
816
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
817 def test_rawvalue(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
818 self.test_value(raw=True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
819
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
820 def test_getobj_getlock(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
821 if self.TYPE != 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
822 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
823
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
824 val1 = self.Value('i', 5)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
825 lock1 = val1.get_lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
826 obj1 = val1.get_obj()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
827
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
828 val2 = self.Value('i', 5, lock=None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
829 lock2 = val2.get_lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
830 obj2 = val2.get_obj()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
831
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
832 lock = self.Lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
833 val3 = self.Value('i', 5, lock=lock)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
834 lock3 = val3.get_lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
835 obj3 = val3.get_obj()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
836 self.assertEqual(lock, lock3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
837
50857
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
838 arr4 = self.Value('i', 5, lock=False)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
839 self.assertFalse(hasattr(arr4, 'get_lock'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
840 self.assertFalse(hasattr(arr4, 'get_obj'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
841
50857
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
842 self.assertRaises(AttributeError, self.Value, 'i', 5, lock='navalue')
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
843
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
844 arr5 = self.RawValue('i', 5)
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
845 self.assertFalse(hasattr(arr5, 'get_lock'))
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
846 self.assertFalse(hasattr(arr5, 'get_obj'))
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
847
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
848
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
849 class _TestArray(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
850
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
851 def f(self, seq):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
852 for i in range(1, len(seq)):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
853 seq[i] += seq[i-1]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
854
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
855 def test_array(self, raw=False):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
856 if self.TYPE != 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
857 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
858
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
859 seq = [680, 626, 934, 821, 150, 233, 548, 982, 714, 831]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
860 if raw:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
861 arr = self.RawArray('i', seq)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
862 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
863 arr = self.Array('i', seq)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
864
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
865 self.assertEqual(len(arr), len(seq))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
866 self.assertEqual(arr[3], seq[3])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
867 self.assertEqual(list(arr[2:7]), list(seq[2:7]))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
868
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
869 arr[4:8] = seq[4:8] = array.array('i', [1, 2, 3, 4])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
870
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
871 self.assertEqual(list(arr[:]), seq)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
872
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
873 self.f(seq)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
874
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
875 p = self.Process(target=self.f, args=(arr,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
876 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
877 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
878
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
879 self.assertEqual(list(arr[:]), seq)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
880
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
881 def test_rawarray(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
882 self.test_array(raw=True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
883
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
884 def test_getobj_getlock_obj(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
885 if self.TYPE != 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
886 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
887
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
888 arr1 = self.Array('i', range(10))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
889 lock1 = arr1.get_lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
890 obj1 = arr1.get_obj()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
891
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
892 arr2 = self.Array('i', range(10), lock=None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
893 lock2 = arr2.get_lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
894 obj2 = arr2.get_obj()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
895
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
896 lock = self.Lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
897 arr3 = self.Array('i', range(10), lock=lock)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
898 lock3 = arr3.get_lock()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
899 obj3 = arr3.get_obj()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
900 self.assertEqual(lock, lock3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
901
50857
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
902 arr4 = self.Array('i', range(10), lock=False)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
903 self.assertFalse(hasattr(arr4, 'get_lock'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
904 self.assertFalse(hasattr(arr4, 'get_obj'))
50857
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
905 self.assertRaises(AttributeError,
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
906 self.Array, 'i', range(10), lock='notalock')
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
907
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
908 arr5 = self.RawArray('i', range(10))
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
909 self.assertFalse(hasattr(arr5, 'get_lock'))
e91f64f5d955 Merged revisions 68708 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49346
diff changeset
910 self.assertFalse(hasattr(arr5, 'get_obj'))
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
911
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
912 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
913 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
914 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
915
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
916 class _TestContainers(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
917
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
918 ALLOWED_TYPES = ('manager',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
919
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
920 def test_list(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
921 a = self.list(range(10))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
922 self.assertEqual(a[:], range(10))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
923
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
924 b = self.list()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
925 self.assertEqual(b[:], [])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
926
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
927 b.extend(range(5))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
928 self.assertEqual(b[:], range(5))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
929
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
930 self.assertEqual(b[2], 2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
931 self.assertEqual(b[2:10], [2,3,4])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
932
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
933 b *= 2
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
934 self.assertEqual(b[:], [0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
935
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
936 self.assertEqual(b + [5, 6], [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
937
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
938 self.assertEqual(a[:], range(10))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
939
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
940 d = [a, b]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
941 e = self.list(d)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
942 self.assertEqual(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
943 e[:],
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
944 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
945 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
946
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
947 f = self.list([a])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
948 a.append('hello')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
949 self.assertEqual(f[:], [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello']])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
950
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
951 def test_dict(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
952 d = self.dict()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
953 indices = range(65, 70)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
954 for i in indices:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
955 d[i] = chr(i)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
956 self.assertEqual(d.copy(), dict((i, chr(i)) for i in indices))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
957 self.assertEqual(sorted(d.keys()), indices)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
958 self.assertEqual(sorted(d.values()), [chr(i) for i in indices])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
959 self.assertEqual(sorted(d.items()), [(i, chr(i)) for i in indices])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
960
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
961 def test_namespace(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
962 n = self.Namespace()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
963 n.name = 'Bob'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
964 n.job = 'Builder'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
965 n._hidden = 'hidden'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
966 self.assertEqual((n.name, n.job), ('Bob', 'Builder'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
967 del n.job
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
968 self.assertEqual(str(n), "Namespace(name='Bob')")
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
969 self.assertTrue(hasattr(n, 'name'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
970 self.assertTrue(not hasattr(n, 'job'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
971
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
972 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
973 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
974 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
975
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
976 def sqr(x, wait=0.0):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
977 time.sleep(wait)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
978 return x*x
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
979 class _TestPool(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
980
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
981 def test_apply(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
982 papply = self.pool.apply
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
983 self.assertEqual(papply(sqr, (5,)), sqr(5))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
984 self.assertEqual(papply(sqr, (), {'x':3}), sqr(x=3))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
985
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
986 def test_map(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
987 pmap = self.pool.map
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
988 self.assertEqual(pmap(sqr, range(10)), map(sqr, range(10)))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
989 self.assertEqual(pmap(sqr, range(100), chunksize=20),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
990 map(sqr, range(100)))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
991
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
992 def test_async(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
993 res = self.pool.apply_async(sqr, (7, TIMEOUT1,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
994 get = TimingWrapper(res.get)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
995 self.assertEqual(get(), 49)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
996 self.assertTimingAlmostEqual(get.elapsed, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
997
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
998 def test_async_timeout(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
999 res = self.pool.apply_async(sqr, (6, TIMEOUT2 + 0.2))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1000 get = TimingWrapper(res.get)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1001 self.assertRaises(multiprocessing.TimeoutError, get, timeout=TIMEOUT2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1002 self.assertTimingAlmostEqual(get.elapsed, TIMEOUT2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1003
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1004 def test_imap(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1005 it = self.pool.imap(sqr, range(10))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1006 self.assertEqual(list(it), map(sqr, range(10)))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1007
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1008 it = self.pool.imap(sqr, range(10))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1009 for i in range(10):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1010 self.assertEqual(it.next(), i*i)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1011 self.assertRaises(StopIteration, it.next)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1012
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1013 it = self.pool.imap(sqr, range(1000), chunksize=100)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1014 for i in range(1000):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1015 self.assertEqual(it.next(), i*i)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1016 self.assertRaises(StopIteration, it.next)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1017
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1018 def test_imap_unordered(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1019 it = self.pool.imap_unordered(sqr, range(1000))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1020 self.assertEqual(sorted(it), map(sqr, range(1000)))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1021
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1022 it = self.pool.imap_unordered(sqr, range(1000), chunksize=53)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1023 self.assertEqual(sorted(it), map(sqr, range(1000)))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1024
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1025 def test_make_pool(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1026 p = multiprocessing.Pool(3)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1027 self.assertEqual(3, len(p._pool))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1028 p.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1029 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1030
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1031 def test_terminate(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1032 if self.TYPE == 'manager':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1033 # On Unix a forked process increfs each shared object to
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1034 # which its parent process held a reference. If the
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1035 # forked process gets terminated then there is likely to
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1036 # be a reference leak. So to prevent
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1037 # _TestZZZNumberOfObjects from failing we skip this test
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1038 # when using a manager.
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1039 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1040
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1041 result = self.pool.map_async(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1042 time.sleep, [0.1 for i in range(10000)], chunksize=1
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1043 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1044 self.pool.terminate()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1045 join = TimingWrapper(self.pool.join)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1046 join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1047 self.assertTrue(join.elapsed < 0.2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1048 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1049 # Test that manager has expected number of shared objects left
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1050 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1051
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1052 class _TestZZZNumberOfObjects(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1053 # Because test cases are sorted alphabetically, this one will get
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1054 # run after all the other tests for the manager. It tests that
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1055 # there have been no "reference leaks" for the manager's shared
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1056 # objects. Note the comment in _TestPool.test_terminate().
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1057 ALLOWED_TYPES = ('manager',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1058
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1059 def test_number_of_objects(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1060 EXPECTED_NUMBER = 1 # the pool object is still alive
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1061 multiprocessing.active_children() # discard dead process objs
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1062 gc.collect() # do garbage collection
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1063 refs = self.manager._number_of_objects()
50960
d274062816ff Issue 5009: multiprocessing: failure in manager._debug_info()
Jesse Noller <jnoller@gmail.com>
parents: 52445
diff changeset
1064 debug_info = self.manager._debug_info()
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1065 if refs != EXPECTED_NUMBER:
48004
6dd520e1bc0d Fix issue 3395, update _debugInfo to be _debug_info
Jesse Noller <jnoller@gmail.com>
parents: 47946
diff changeset
1066 print self.manager._debug_info()
50960
d274062816ff Issue 5009: multiprocessing: failure in manager._debug_info()
Jesse Noller <jnoller@gmail.com>
parents: 52445
diff changeset
1067 print debug_info
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1068
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1069 self.assertEqual(refs, EXPECTED_NUMBER)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1070
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1071 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1072 # Test of creating a customized manager class
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1073 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1074
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1075 from multiprocessing.managers import BaseManager, BaseProxy, RemoteError
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1076
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1077 class FooBar(object):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1078 def f(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1079 return 'f()'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1080 def g(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1081 raise ValueError
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1082 def _h(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1083 return '_h()'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1084
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1085 def baz():
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1086 for i in xrange(10):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1087 yield i*i
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1088
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1089 class IteratorProxy(BaseProxy):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1090 _exposed_ = ('next', '__next__')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1091 def __iter__(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1092 return self
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1093 def next(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1094 return self._callmethod('next')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1095 def __next__(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1096 return self._callmethod('__next__')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1097
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1098 class MyManager(BaseManager):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1099 pass
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1100
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1101 MyManager.register('Foo', callable=FooBar)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1102 MyManager.register('Bar', callable=FooBar, exposed=('f', '_h'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1103 MyManager.register('baz', callable=baz, proxytype=IteratorProxy)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1104
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1105
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1106 class _TestMyManager(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1107
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1108 ALLOWED_TYPES = ('manager',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1109
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1110 def test_mymanager(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1111 manager = MyManager()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1112 manager.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1113
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1114 foo = manager.Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1115 bar = manager.Bar()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1116 baz = manager.baz()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1117
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1118 foo_methods = [name for name in ('f', 'g', '_h') if hasattr(foo, name)]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1119 bar_methods = [name for name in ('f', 'g', '_h') if hasattr(bar, name)]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1120
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1121 self.assertEqual(foo_methods, ['f', 'g'])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1122 self.assertEqual(bar_methods, ['f', '_h'])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1123
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1124 self.assertEqual(foo.f(), 'f()')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1125 self.assertRaises(ValueError, foo.g)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1126 self.assertEqual(foo._callmethod('f'), 'f()')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1127 self.assertRaises(RemoteError, foo._callmethod, '_h')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1128
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1129 self.assertEqual(bar.f(), 'f()')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1130 self.assertEqual(bar._h(), '_h()')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1131 self.assertEqual(bar._callmethod('f'), 'f()')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1132 self.assertEqual(bar._callmethod('_h'), '_h()')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1133
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1134 self.assertEqual(list(baz), [i*i for i in range(10)])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1135
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1136 manager.shutdown()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1137
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1138 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1139 # Test of connecting to a remote server and using xmlrpclib for serialization
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1140 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1141
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1142 _queue = Queue.Queue()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1143 def get_queue():
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1144 return _queue
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1145
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1146 class QueueManager(BaseManager):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1147 '''manager class used by server process'''
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1148 QueueManager.register('get_queue', callable=get_queue)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1149
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1150 class QueueManager2(BaseManager):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1151 '''manager class which specifies the same interface as QueueManager'''
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1152 QueueManager2.register('get_queue')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1153
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1154
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1155 SERIALIZER = 'xmlrpclib'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1156
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1157 class _TestRemoteManager(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1158
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1159 ALLOWED_TYPES = ('manager',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1160
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1161 def _putter(self, address, authkey):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1162 manager = QueueManager2(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1163 address=address, authkey=authkey, serializer=SERIALIZER
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1164 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1165 manager.connect()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1166 queue = manager.get_queue()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1167 queue.put(('hello world', None, True, 2.25))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1168
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1169 def test_remote(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1170 authkey = os.urandom(32)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1171
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1172 manager = QueueManager(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1173 address=('localhost', 0), authkey=authkey, serializer=SERIALIZER
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1174 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1175 manager.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1176
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1177 p = self.Process(target=self._putter, args=(manager.address, authkey))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1178 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1179
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1180 manager2 = QueueManager2(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1181 address=manager.address, authkey=authkey, serializer=SERIALIZER
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1182 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1183 manager2.connect()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1184 queue = manager2.get_queue()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1185
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1186 # Note that xmlrpclib will deserialize object as a list not a tuple
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1187 self.assertEqual(queue.get(), ['hello world', None, True, 2.25])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1188
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1189 # Because we are using xmlrpclib for serialization instead of
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1190 # pickle this will cause a serialization error.
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1191 self.assertRaises(Exception, queue.put, time.sleep)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1192
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1193 # Make queue finalizer run before the server is stopped
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1194 del queue
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1195 manager.shutdown()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1196
52441
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1197 class _TestManagerRestart(BaseTestCase):
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1198
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1199 def _putter(self, address, authkey):
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1200 manager = QueueManager(
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1201 address=address, authkey=authkey, serializer=SERIALIZER)
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1202 manager.connect()
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1203 queue = manager.get_queue()
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1204 queue.put('hello world')
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1205
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1206 def test_rapid_restart(self):
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1207 authkey = os.urandom(32)
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1208 manager = QueueManager(
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1209 address=('localhost', 9999), authkey=authkey, serializer=SERIALIZER)
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1210 manager.start()
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1211
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1212 p = self.Process(target=self._putter, args=(manager.address, authkey))
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1213 p.start()
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1214 queue = manager.get_queue()
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1215 self.assertEqual(queue.get(), 'hello world')
52477
fcfc2c994a4a finalize the queue prior to shutdown
Jesse Noller <jnoller@gmail.com>
parents: 52441
diff changeset
1216 del queue
52441
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1217 manager.shutdown()
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1218 manager = QueueManager(
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1219 address=('localhost', 9999), authkey=authkey, serializer=SERIALIZER)
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1220 manager.start()
52477
fcfc2c994a4a finalize the queue prior to shutdown
Jesse Noller <jnoller@gmail.com>
parents: 52441
diff changeset
1221 manager.shutdown()
52441
965230b3379f Issue 5177: use socket.SO_REUSEADDR on multiprocessing SocketManager sockets
Jesse Noller <jnoller@gmail.com>
parents: 52355
diff changeset
1222
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1223 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1224 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1225 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1226
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1227 SENTINEL = latin('')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1228
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1229 class _TestConnection(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1230
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1231 ALLOWED_TYPES = ('processes', 'threads')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1232
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1233 def _echo(self, conn):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1234 for msg in iter(conn.recv_bytes, SENTINEL):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1235 conn.send_bytes(msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1236 conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1237
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1238 def test_connection(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1239 conn, child_conn = self.Pipe()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1240
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1241 p = self.Process(target=self._echo, args=(child_conn,))
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
1242 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1243 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1244
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1245 seq = [1, 2.25, None]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1246 msg = latin('hello world')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1247 longmsg = msg * 10
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1248 arr = array.array('i', range(4))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1249
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1250 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1251 self.assertEqual(type(conn.fileno()), int)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1252
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1253 self.assertEqual(conn.send(seq), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1254 self.assertEqual(conn.recv(), seq)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1255
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1256 self.assertEqual(conn.send_bytes(msg), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1257 self.assertEqual(conn.recv_bytes(), msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1258
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1259 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1260 buffer = array.array('i', [0]*10)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1261 expected = list(arr) + [0] * (10 - len(arr))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1262 self.assertEqual(conn.send_bytes(arr), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1263 self.assertEqual(conn.recv_bytes_into(buffer),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1264 len(arr) * buffer.itemsize)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1265 self.assertEqual(list(buffer), expected)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1266
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1267 buffer = array.array('i', [0]*10)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1268 expected = [0] * 3 + list(arr) + [0] * (10 - 3 - len(arr))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1269 self.assertEqual(conn.send_bytes(arr), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1270 self.assertEqual(conn.recv_bytes_into(buffer, 3 * buffer.itemsize),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1271 len(arr) * buffer.itemsize)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1272 self.assertEqual(list(buffer), expected)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1273
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1274 buffer = bytearray(latin(' ' * 40))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1275 self.assertEqual(conn.send_bytes(longmsg), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1276 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1277 res = conn.recv_bytes_into(buffer)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1278 except multiprocessing.BufferTooShort, e:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1279 self.assertEqual(e.args, (longmsg,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1280 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1281 self.fail('expected BufferTooShort, got %s' % res)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1282
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1283 poll = TimingWrapper(conn.poll)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1284
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1285 self.assertEqual(poll(), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1286 self.assertTimingAlmostEqual(poll.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1287
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1288 self.assertEqual(poll(TIMEOUT1), False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1289 self.assertTimingAlmostEqual(poll.elapsed, TIMEOUT1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1290
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1291 conn.send(None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1292
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1293 self.assertEqual(poll(TIMEOUT1), True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1294 self.assertTimingAlmostEqual(poll.elapsed, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1295
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1296 self.assertEqual(conn.recv(), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1297
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1298 really_big_msg = latin('X') * (1024 * 1024 * 16) # 16Mb
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1299 conn.send_bytes(really_big_msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1300 self.assertEqual(conn.recv_bytes(), really_big_msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1301
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1302 conn.send_bytes(SENTINEL) # tell child to quit
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1303 child_conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1304
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1305 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1306 self.assertEqual(conn.readable, True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1307 self.assertEqual(conn.writable, True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1308 self.assertRaises(EOFError, conn.recv)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1309 self.assertRaises(EOFError, conn.recv_bytes)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1310
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1311 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1312
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1313 def test_duplex_false(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1314 reader, writer = self.Pipe(duplex=False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1315 self.assertEqual(writer.send(1), None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1316 self.assertEqual(reader.recv(), 1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1317 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1318 self.assertEqual(reader.readable, True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1319 self.assertEqual(reader.writable, False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1320 self.assertEqual(writer.readable, False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1321 self.assertEqual(writer.writable, True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1322 self.assertRaises(IOError, reader.send, 2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1323 self.assertRaises(IOError, writer.recv)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1324 self.assertRaises(IOError, writer.poll)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1325
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1326 def test_spawn_close(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1327 # We test that a pipe connection can be closed by parent
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1328 # process immediately after child is spawned. On Windows this
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1329 # would have sometimes failed on old versions because
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1330 # child_conn would be closed before the child got a chance to
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1331 # duplicate it.
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1332 conn, child_conn = self.Pipe()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1333
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1334 p = self.Process(target=self._echo, args=(child_conn,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1335 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1336 child_conn.close() # this might complete before child initializes
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1337
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1338 msg = latin('hello')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1339 conn.send_bytes(msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1340 self.assertEqual(conn.recv_bytes(), msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1341
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1342 conn.send_bytes(SENTINEL)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1343 conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1344 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1345
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1346 def test_sendbytes(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1347 if self.TYPE != 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1348 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1349
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1350 msg = latin('abcdefghijklmnopqrstuvwxyz')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1351 a, b = self.Pipe()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1352
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1353 a.send_bytes(msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1354 self.assertEqual(b.recv_bytes(), msg)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1355
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1356 a.send_bytes(msg, 5)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1357 self.assertEqual(b.recv_bytes(), msg[5:])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1358
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1359 a.send_bytes(msg, 7, 8)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1360 self.assertEqual(b.recv_bytes(), msg[7:7+8])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1361
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1362 a.send_bytes(msg, 26)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1363 self.assertEqual(b.recv_bytes(), latin(''))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1364
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1365 a.send_bytes(msg, 26, 0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1366 self.assertEqual(b.recv_bytes(), latin(''))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1367
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1368 self.assertRaises(ValueError, a.send_bytes, msg, 27)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1369
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1370 self.assertRaises(ValueError, a.send_bytes, msg, 22, 5)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1371
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1372 self.assertRaises(ValueError, a.send_bytes, msg, 26, 1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1373
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1374 self.assertRaises(ValueError, a.send_bytes, msg, -1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1375
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1376 self.assertRaises(ValueError, a.send_bytes, msg, 4, -1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1377
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1378 class _TestListenerClient(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1379
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1380 ALLOWED_TYPES = ('processes', 'threads')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1381
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1382 def _test(self, address):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1383 conn = self.connection.Client(address)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1384 conn.send('hello')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1385 conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1386
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1387 def test_listener_client(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1388 for family in self.connection.families:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1389 l = self.connection.Listener(family=family)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1390 p = self.Process(target=self._test, args=(l.address,))
48302
7ea805d879e1 issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents: 48291
diff changeset
1391 p.daemon = True
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1392 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1393 conn = l.accept()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1394 self.assertEqual(conn.recv(), 'hello')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1395 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1396 l.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1397 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1398 # Test of sending connection and socket objects between processes
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1399 #
47739
f8a8894da77a reduce the test_multiprocessing load to ones that shouldn't hang. These will be reenabled gradually as we find the problems.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
1400 """
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1401 class _TestPicklingConnections(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1402
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1403 ALLOWED_TYPES = ('processes',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1404
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1405 def _listener(self, conn, families):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1406 for fam in families:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1407 l = self.connection.Listener(family=fam)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1408 conn.send(l.address)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1409 new_conn = l.accept()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1410 conn.send(new_conn)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1411
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1412 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1413 l = socket.socket()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1414 l.bind(('localhost', 0))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1415 conn.send(l.getsockname())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1416 l.listen(1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1417 new_conn, addr = l.accept()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1418 conn.send(new_conn)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1419
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1420 conn.recv()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1421
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1422 def _remote(self, conn):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1423 for (address, msg) in iter(conn.recv, None):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1424 client = self.connection.Client(address)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1425 client.send(msg.upper())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1426 client.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1427
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1428 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1429 address, msg = conn.recv()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1430 client = socket.socket()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1431 client.connect(address)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1432 client.sendall(msg.upper())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1433 client.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1434
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1435 conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1436
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1437 def test_pickling(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1438 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1439 multiprocessing.allow_connection_pickling()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1440 except ImportError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1441 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1442
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1443 families = self.connection.families
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1444
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1445 lconn, lconn0 = self.Pipe()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1446 lp = self.Process(target=self._listener, args=(lconn0, families))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1447 lp.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1448 lconn0.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1449
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1450 rconn, rconn0 = self.Pipe()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1451 rp = self.Process(target=self._remote, args=(rconn0,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1452 rp.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1453 rconn0.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1454
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1455 for fam in families:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1456 msg = ('This connection uses family %s' % fam).encode('ascii')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1457 address = lconn.recv()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1458 rconn.send((address, msg))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1459 new_conn = lconn.recv()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1460 self.assertEqual(new_conn.recv(), msg.upper())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1461
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1462 rconn.send(None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1463
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1464 if self.TYPE == 'processes':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1465 msg = latin('This connection uses a normal socket')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1466 address = lconn.recv()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1467 rconn.send((address, msg))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1468 if hasattr(socket, 'fromfd'):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1469 new_conn = lconn.recv()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1470 self.assertEqual(new_conn.recv(100), msg.upper())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1471 else:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1472 # XXX On Windows with Py2.6 need to backport fromfd()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1473 discard = lconn.recv_bytes()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1474
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1475 lconn.send(None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1476
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1477 rconn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1478 lconn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1479
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1480 lp.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1481 rp.join()
47739
f8a8894da77a reduce the test_multiprocessing load to ones that shouldn't hang. These will be reenabled gradually as we find the problems.
Benjamin Peterson <benjamin@python.org>
parents: 47694
diff changeset
1482 """
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1483 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1484 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1485 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1486
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1487 class _TestHeap(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1488
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1489 ALLOWED_TYPES = ('processes',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1490
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1491 def test_heap(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1492 iterations = 5000
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1493 maxblocks = 50
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1494 blocks = []
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1495
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1496 # create and destroy lots of blocks of different sizes
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1497 for i in xrange(iterations):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1498 size = int(random.lognormvariate(0, 1) * 1000)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1499 b = multiprocessing.heap.BufferWrapper(size)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1500 blocks.append(b)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1501 if len(blocks) > maxblocks:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1502 i = random.randrange(maxblocks)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1503 del blocks[i]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1504
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1505 # get the heap object
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1506 heap = multiprocessing.heap.BufferWrapper._heap
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1507
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1508 # verify the state of the heap
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1509 all = []
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1510 occupied = 0
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1511 for L in heap._len_to_seq.values():
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1512 for arena, start, stop in L:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1513 all.append((heap._arenas.index(arena), start, stop,
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1514 stop-start, 'free'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1515 for arena, start, stop in heap._allocated_blocks:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1516 all.append((heap._arenas.index(arena), start, stop,
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1517 stop-start, 'occupied'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1518 occupied += (stop-start)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1519
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1520 all.sort()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1521
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1522 for i in range(len(all)-1):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1523 (arena, start, stop) = all[i][:3]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1524 (narena, nstart, nstop) = all[i+1][:3]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1525 self.assertTrue((arena != narena and nstart == 0) or
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1526 (stop == nstart))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1527
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1528 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1529 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1530 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1531
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1532 try:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1533 from ctypes import Structure, Value, copy, c_int, c_double
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1534 except ImportError:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1535 Structure = object
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1536 c_int = c_double = None
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1537
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1538 class _Foo(Structure):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1539 _fields_ = [
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1540 ('x', c_int),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1541 ('y', c_double)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1542 ]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1543
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1544 class _TestSharedCTypes(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1545
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1546 ALLOWED_TYPES = ('processes',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1547
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1548 def _double(self, x, y, foo, arr, string):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1549 x.value *= 2
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1550 y.value *= 2
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1551 foo.x *= 2
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1552 foo.y *= 2
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1553 string.value *= 2
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1554 for i in range(len(arr)):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1555 arr[i] *= 2
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1556
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1557 def test_sharedctypes(self, lock=False):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1558 if c_int is None:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1559 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1560
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1561 x = Value('i', 7, lock=lock)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1562 y = Value(ctypes.c_double, 1.0/3.0, lock=lock)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1563 foo = Value(_Foo, 3, 2, lock=lock)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1564 arr = Array('d', range(10), lock=lock)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1565 string = Array('c', 20, lock=lock)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1566 string.value = 'hello'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1567
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1568 p = self.Process(target=self._double, args=(x, y, foo, arr, string))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1569 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1570 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1571
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1572 self.assertEqual(x.value, 14)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1573 self.assertAlmostEqual(y.value, 2.0/3.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1574 self.assertEqual(foo.x, 6)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1575 self.assertAlmostEqual(foo.y, 4.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1576 for i in range(10):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1577 self.assertAlmostEqual(arr[i], i*2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1578 self.assertEqual(string.value, latin('hellohello'))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1579
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1580 def test_synchronize(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1581 self.test_sharedctypes(lock=True)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1582
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1583 def test_copy(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1584 if c_int is None:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1585 return
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1586
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1587 foo = _Foo(2, 5.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1588 bar = copy(foo)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1589 foo.x = 0
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1590 foo.y = 0
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1591 self.assertEqual(bar.x, 2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1592 self.assertAlmostEqual(bar.y, 5.0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1593
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1594 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1595 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1596 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1597
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1598 class _TestFinalize(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1599
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1600 ALLOWED_TYPES = ('processes',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1601
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1602 def _test_finalize(self, conn):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1603 class Foo(object):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1604 pass
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1605
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1606 a = Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1607 util.Finalize(a, conn.send, args=('a',))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1608 del a # triggers callback for a
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1609
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1610 b = Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1611 close_b = util.Finalize(b, conn.send, args=('b',))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1612 close_b() # triggers callback for b
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1613 close_b() # does nothing because callback has already been called
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1614 del b # does nothing because callback has already been called
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1615
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1616 c = Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1617 util.Finalize(c, conn.send, args=('c',))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1618
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1619 d10 = Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1620 util.Finalize(d10, conn.send, args=('d10',), exitpriority=1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1621
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1622 d01 = Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1623 util.Finalize(d01, conn.send, args=('d01',), exitpriority=0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1624 d02 = Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1625 util.Finalize(d02, conn.send, args=('d02',), exitpriority=0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1626 d03 = Foo()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1627 util.Finalize(d03, conn.send, args=('d03',), exitpriority=0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1628
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1629 util.Finalize(None, conn.send, args=('e',), exitpriority=-10)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1630
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1631 util.Finalize(None, conn.send, args=('STOP',), exitpriority=-100)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1632
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1633 # call mutliprocessing's cleanup function then exit process without
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1634 # garbage collecting locals
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1635 util._exit_function()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1636 conn.close()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1637 os._exit(0)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1638
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1639 def test_finalize(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1640 conn, child_conn = self.Pipe()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1641
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1642 p = self.Process(target=self._test_finalize, args=(child_conn,))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1643 p.start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1644 p.join()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1645
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1646 result = [obj for obj in iter(conn.recv, 'STOP')]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1647 self.assertEqual(result, ['a', 'b', 'd10', 'd03', 'd02', 'd01', 'e'])
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1648
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1649 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1650 # Test that from ... import * works for each module
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1651 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1652
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1653 class _TestImportStar(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1654
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1655 ALLOWED_TYPES = ('processes',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1656
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1657 def test_import(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1658 modules = (
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1659 'multiprocessing', 'multiprocessing.connection',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1660 'multiprocessing.heap', 'multiprocessing.managers',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1661 'multiprocessing.pool', 'multiprocessing.process',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1662 'multiprocessing.reduction', 'multiprocessing.sharedctypes',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1663 'multiprocessing.synchronize', 'multiprocessing.util'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1664 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1665
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1666 for name in modules:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1667 __import__(name)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1668 mod = sys.modules[name]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1669
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1670 for attr in getattr(mod, '__all__', ()):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1671 self.assertTrue(
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1672 hasattr(mod, attr),
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1673 '%r does not have attribute %r' % (mod, attr)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1674 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1675
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1676 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1677 # Quick test that logging works -- does not test logging output
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1678 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1679
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1680 class _TestLogging(BaseTestCase):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1681
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1682 ALLOWED_TYPES = ('processes',)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1683
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1684 def test_enable_logging(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1685 logger = multiprocessing.get_logger()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1686 logger.setLevel(util.SUBWARNING)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1687 self.assertTrue(logger is not None)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1688 logger.debug('this will not be printed')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1689 logger.info('nor will this')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1690 logger.setLevel(LOG_LEVEL)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1691
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1692 def _test_level(self, conn):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1693 logger = multiprocessing.get_logger()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1694 conn.send(logger.getEffectiveLevel())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1695
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1696 def test_level(self):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1697 LEVEL1 = 32
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1698 LEVEL2 = 37
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1699
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1700 logger = multiprocessing.get_logger()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1701 root_logger = logging.getLogger()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1702 root_level = root_logger.level
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1703
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1704 reader, writer = multiprocessing.Pipe(duplex=False)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1705
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1706 logger.setLevel(LEVEL1)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1707 self.Process(target=self._test_level, args=(writer,)).start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1708 self.assertEqual(LEVEL1, reader.recv())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1709
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1710 logger.setLevel(logging.NOTSET)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1711 root_logger.setLevel(LEVEL2)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1712 self.Process(target=self._test_level, args=(writer,)).start()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1713 self.assertEqual(LEVEL2, reader.recv())
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1714
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1715 root_logger.setLevel(root_level)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1716 logger.setLevel(level=LOG_LEVEL)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1717
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1718 #
52445
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1719 # Test to verify handle verification, see issue 3321
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1720 #
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1721
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1722 class TestInvalidHandle(unittest.TestCase):
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1723
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1724 def test_invalid_handles(self):
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1725 if WIN32:
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1726 return
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1727 conn = _multiprocessing.Connection(44977608)
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1728 self.assertRaises(IOError, conn.poll)
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1729 self.assertRaises(IOError, _multiprocessing.Connection, -1)
b4ad5948e197 Merge 68768 to maint
Jesse Noller <jnoller@gmail.com>
parents: 50857
diff changeset
1730 #
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1731 # Functions used to create test cases from the base ones in this module
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1732 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1733
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1734 def get_attributes(Source, names):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1735 d = {}
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1736 for name in names:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1737 obj = getattr(Source, name)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1738 if type(obj) == type(get_attributes):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1739 obj = staticmethod(obj)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1740 d[name] = obj
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1741 return d
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1742
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1743 def create_test_cases(Mixin, type):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1744 result = {}
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1745 glob = globals()
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1746 Type = type[0].upper() + type[1:]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1747
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1748 for name in glob.keys():
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1749 if name.startswith('_Test'):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1750 base = glob[name]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1751 if type in base.ALLOWED_TYPES:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1752 newname = 'With' + Type + name[1:]
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1753 class Temp(base, unittest.TestCase, Mixin):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1754 pass
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1755 result[newname] = Temp
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1756 Temp.__name__ = newname
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1757 Temp.__module__ = Mixin.__module__
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1758 return result
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1759
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1760 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1761 # Create test cases
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1762 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1763
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1764 class ProcessesMixin(object):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1765 TYPE = 'processes'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1766 Process = multiprocessing.Process
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1767 locals().update(get_attributes(multiprocessing, (
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1768 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1769 'Condition', 'Event', 'Value', 'Array', 'RawValue',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1770 'RawArray', 'current_process', 'active_children', 'Pipe',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1771 'connection', 'JoinableQueue'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1772 )))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1773
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1774 testcases_processes = create_test_cases(ProcessesMixin, type='processes')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1775 globals().update(testcases_processes)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1776
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1777
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1778 class ManagerMixin(object):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1779 TYPE = 'manager'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1780 Process = multiprocessing.Process
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1781 manager = object.__new__(multiprocessing.managers.SyncManager)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1782 locals().update(get_attributes(manager, (
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1783 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1784 'Condition', 'Event', 'Value', 'Array', 'list', 'dict',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1785 'Namespace', 'JoinableQueue'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1786 )))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1787
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1788 testcases_manager = create_test_cases(ManagerMixin, type='manager')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1789 globals().update(testcases_manager)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1790
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1791
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1792 class ThreadsMixin(object):
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1793 TYPE = 'threads'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1794 Process = multiprocessing.dummy.Process
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1795 locals().update(get_attributes(multiprocessing.dummy, (
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1796 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1797 'Condition', 'Event', 'Value', 'Array', 'current_process',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1798 'active_children', 'Pipe', 'connection', 'dict', 'list',
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1799 'Namespace', 'JoinableQueue'
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1800 )))
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1801
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1802 testcases_threads = create_test_cases(ThreadsMixin, type='threads')
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1803 globals().update(testcases_threads)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1804
48864
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1805 class OtherTest(unittest.TestCase):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1806 # TODO: add more tests for deliver/answer challenge.
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1807 def test_deliver_challenge_auth_failure(self):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1808 class _FakeConnection(object):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1809 def recv_bytes(self, size):
48866
6fac636133f0 Use bytes as return type from recv_bytes() methods. Not sure why this only
Neal Norwitz <nnorwitz@gmail.com>
parents: 48864
diff changeset
1810 return b'something bogus'
48864
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1811 def send_bytes(self, data):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1812 pass
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1813 self.assertRaises(multiprocessing.AuthenticationError,
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1814 multiprocessing.connection.deliver_challenge,
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1815 _FakeConnection(), b'abc')
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1816
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1817 def test_answer_challenge_auth_failure(self):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1818 class _FakeConnection(object):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1819 def __init__(self):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1820 self.count = 0
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1821 def recv_bytes(self, size):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1822 self.count += 1
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1823 if self.count == 1:
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1824 return multiprocessing.connection.CHALLENGE
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1825 elif self.count == 2:
48866
6fac636133f0 Use bytes as return type from recv_bytes() methods. Not sure why this only
Neal Norwitz <nnorwitz@gmail.com>
parents: 48864
diff changeset
1826 return b'something bogus'
6fac636133f0 Use bytes as return type from recv_bytes() methods. Not sure why this only
Neal Norwitz <nnorwitz@gmail.com>
parents: 48864
diff changeset
1827 return b''
48864
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1828 def send_bytes(self, data):
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1829 pass
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1830 self.assertRaises(multiprocessing.AuthenticationError,
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1831 multiprocessing.connection.answer_challenge,
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1832 _FakeConnection(), b'abc')
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1833
52727
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1834 #
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1835 # Test Manager.start()/Pool.__init__() initializer feature - see issue 5585
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1836 #
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1837
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1838 def initializer(ns):
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1839 ns.test += 1
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1840
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1841 class TestInitializers(unittest.TestCase):
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1842 def setUp(self):
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1843 self.mgr = multiprocessing.Manager()
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1844 self.ns = self.mgr.Namespace()
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1845 self.ns.test = 0
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1846
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1847 def tearDown(self):
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1848 self.mgr.shutdown()
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1849
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1850 def test_manager_initializer(self):
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1851 m = multiprocessing.managers.SyncManager()
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1852 self.assertRaises(TypeError, m.start, 1)
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1853 m.start(initializer, (self.ns,))
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1854 self.assertEqual(self.ns.test, 1)
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1855 m.shutdown()
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1856
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1857 def test_pool_initializer(self):
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1858 self.assertRaises(TypeError, multiprocessing.Pool, initializer=1)
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1859 p = multiprocessing.Pool(1, initializer, (self.ns,))
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1860 p.close()
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1861 p.join()
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1862 self.assertEqual(self.ns.test, 1)
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1863
71f6129ddad4 Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Jesse Noller <jnoller@gmail.com>
parents: 52635
diff changeset
1864 testcases_other = [OtherTest, TestInvalidHandle, TestInitializers]
48864
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1865
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1866 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1867 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1868 #
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1869
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1870 def test_main(run=None):
47760
6b7ddc0fda93 Attempt RLock creation prior to test execution on Linux, if it raises the OSError in issue 3111, skip the test suite
Jesse Noller <jnoller@gmail.com>
parents: 47754
diff changeset
1871 if sys.platform.startswith("linux"):
6b7ddc0fda93 Attempt RLock creation prior to test execution on Linux, if it raises the OSError in issue 3111, skip the test suite
Jesse Noller <jnoller@gmail.com>
parents: 47754
diff changeset
1872 try:
6b7ddc0fda93 Attempt RLock creation prior to test execution on Linux, if it raises the OSError in issue 3111, skip the test suite
Jesse Noller <jnoller@gmail.com>
parents: 47754
diff changeset
1873 lock = multiprocessing.RLock()
6b7ddc0fda93 Attempt RLock creation prior to test execution on Linux, if it raises the OSError in issue 3111, skip the test suite
Jesse Noller <jnoller@gmail.com>
parents: 47754
diff changeset
1874 except OSError:
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
1875 raise unittest.SkipTest("OSError raises on RLock creation, see issue 3111!")
47753
a1741617f06f skip test_multiprocessing when /dev/shm doesn't exist. This seems to be a chroot problem in the buildbot env.
Benjamin Peterson <benjamin@python.org>
parents: 47739
diff changeset
1876
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1877 if run is None:
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1878 from test.test_support import run_unittest as run
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1879
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1880 util.get_temp_dir() # creates temp directory for use by all processes
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1881
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1882 multiprocessing.get_logger().setLevel(LOG_LEVEL)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1883
47874
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1884 ProcessesMixin.pool = multiprocessing.Pool(4)
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1885 ThreadsMixin.pool = multiprocessing.dummy.Pool(4)
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1886 ManagerMixin.manager.__init__()
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1887 ManagerMixin.manager.start()
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1888 ManagerMixin.pool = ManagerMixin.manager.Pool(4)
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1889
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1890 testcases = (
47874
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1891 sorted(testcases_processes.values(), key=lambda tc:tc.__name__) +
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1892 sorted(testcases_threads.values(), key=lambda tc:tc.__name__) +
48864
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1893 sorted(testcases_manager.values(), key=lambda tc:tc.__name__) +
df624df6efa3 Fix problem reported by pychecker where AuthenticationError wasn't imported.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48302
diff changeset
1894 testcases_other
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1895 )
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1896
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1897 loadTestsFromTestCase = unittest.defaultTestLoader.loadTestsFromTestCase
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1898 suite = unittest.TestSuite(loadTestsFromTestCase(tc) for tc in testcases)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1899 run(suite)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1900
47874
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1901 ThreadsMixin.pool.terminate()
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1902 ProcessesMixin.pool.terminate()
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1903 ManagerMixin.pool.terminate()
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1904 ManagerMixin.manager.shutdown()
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1905
47874
832ae513d344 Reenable the manager tests with Amaury's threading fix
Jesse Noller <jnoller@gmail.com>
parents: 47761
diff changeset
1906 del ProcessesMixin.pool, ThreadsMixin.pool, ManagerMixin.pool
47694
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1907
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1908 def main():
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1909 test_main(unittest.TextTestRunner(verbosity=2).run)
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1910
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1911 if __name__ == '__main__':
af8873df0322 convert multiprocessing to unix line endings
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
1912 main()