|
21 | 21 | import tempfile |
22 | 22 | from pathlib import Path |
23 | 23 | from typing import Optional, Union |
| 24 | +from collections import defaultdict |
24 | 25 |
|
25 | 26 | from typing_extensions import override |
26 | 27 |
|
@@ -66,14 +67,15 @@ def __init__( |
66 | 67 | If you attempt to stop recording an action which was never started. |
67 | 68 | """ |
68 | 69 | super().__init__(dirpath=dirpath, filename=filename) |
69 | | - self.profiled_actions: dict[str, cProfile.Profile] = {} |
| 70 | + self.profiled_actions: dict[str, cProfile.Profile] = defaultdict(cProfile.Profile) |
70 | 71 | self.line_count_restriction = line_count_restriction |
71 | 72 | self.dump_stats = dump_stats |
72 | 73 |
|
73 | 74 | @override |
74 | 75 | def start(self, action_name: str) -> None: |
75 | | - if action_name not in self.profiled_actions: |
76 | | - self.profiled_actions[action_name] = cProfile.Profile() |
| 76 | + # Disable all profilers before starting a new one |
| 77 | + for pr in self.profiled_actions.values(): |
| 78 | + pr.disable() |
77 | 79 | self.profiled_actions[action_name].enable() |
78 | 80 |
|
79 | 81 | @override |
@@ -114,7 +116,7 @@ def summary(self) -> str: |
114 | 116 | @override |
115 | 117 | def teardown(self, stage: Optional[str]) -> None: |
116 | 118 | super().teardown(stage=stage) |
117 | | - self.profiled_actions = {} |
| 119 | + self.profiled_actions = defaultdict(cProfile.Profile) |
118 | 120 |
|
119 | 121 | def __reduce__(self) -> tuple: |
120 | 122 | # avoids `TypeError: cannot pickle 'cProfile.Profile' object` |
|
0 commit comments