|
4 | 4 | import pstats |
5 | 5 | import unittest |
6 | 6 | import os |
| 7 | +import subprocess |
7 | 8 | import warnings |
8 | 9 | from difflib import unified_diff |
9 | 10 | from io import StringIO |
10 | 11 | from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd |
| 12 | +from test.support import script_helper, os_helper, SHORT_TIMEOUT |
11 | 13 | from contextlib import contextmanager, redirect_stdout |
12 | 14 |
|
13 | 15 | # Suppress deprecation warning for profile module (PEP 799) |
@@ -134,6 +136,39 @@ def test_output_file_when_changing_directory(self): |
134 | 136 |
|
135 | 137 | self.assertTrue(os.path.exists('out.pstats')) |
136 | 138 |
|
| 139 | + def test_profile_multiprocessing(self): |
| 140 | + test_script = ''' |
| 141 | +import multiprocessing |
| 142 | +def worker_proc(x): |
| 143 | + return x * 42 |
| 144 | +
|
| 145 | +def main_proc(): |
| 146 | + p = multiprocessing.Process(target=worker_proc, args=(10,)) |
| 147 | + p.start() |
| 148 | + p.join() |
| 149 | +
|
| 150 | +if __name__ == "__main__": |
| 151 | + main_proc() |
| 152 | +''' |
| 153 | + with os_helper.temp_dir() as temp_dir: |
| 154 | + script = script_helper.make_script( |
| 155 | + temp_dir, 'test_profile_multiprocessing', test_script |
| 156 | + ) |
| 157 | + with script_helper.spawn_python( |
| 158 | + "-m", self.profilermodule.__name__, |
| 159 | + script, |
| 160 | + stderr=subprocess.PIPE, |
| 161 | + text=True |
| 162 | + ) as proc: |
| 163 | + proc.wait(timeout=SHORT_TIMEOUT) |
| 164 | + stdout = proc.stdout.read() |
| 165 | + stderr = proc.stderr.read() |
| 166 | + |
| 167 | + self.assertIn("main_proc", stdout) |
| 168 | + self.assertNotIn("Can't pickle", stderr) |
| 169 | + self.assertNotIn("ModuleSpec", stderr) |
| 170 | + self.assertEqual(proc.returncode, 0) |
| 171 | + |
137 | 172 |
|
138 | 173 | def regenerate_expected_output(filename, cls): |
139 | 174 | filename = filename.rstrip('co') |
|
0 commit comments