Skip to content

Commit 292822b

Browse files
committed
use get_context
1 parent a82a82a commit 292822b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

tests/unit/_utils/test_system.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4-
from multiprocessing import Barrier, Process, Value, synchronize
4+
from multiprocessing import get_context, synchronize
55
from multiprocessing.shared_memory import SharedMemory
66
from typing import TYPE_CHECKING
77

@@ -38,7 +38,9 @@ def test_memory_estimation_does_not_overestimate_due_to_shared_memory() -> None:
3838
equal to additional_memory_size_estimate_per_unshared_memory_child where the additional shared memory is exactly
3939
the same as the unshared memory.
4040
"""
41-
estimated_memory_expectation = Value('b', False) # noqa: FBT003 # Common usage pattern for multiprocessing.Value
41+
42+
ctx = get_context('fork')
43+
estimated_memory_expectation = ctx.Value('b', False) # noqa: FBT003 # Common usage pattern for multiprocessing.Value
4244

4345
def parent_process() -> None:
4446
extra_memory_size = 1024 * 1024 * 100 # 100 MB
@@ -70,8 +72,8 @@ def get_additional_memory_estimation_while_running_processes(
7072
*, target: Callable, count: int = 1, use_shared_memory: bool = False
7173
) -> float:
7274
processes = []
73-
ready = Barrier(parties=count + 1)
74-
measured = Barrier(parties=count + 1)
75+
ready = ctx.Barrier(parties=count + 1)
76+
measured = ctx.Barrier(parties=count + 1)
7577
shared_memory: None | SharedMemory = None
7678
memory_before = get_memory_info().current_size
7779

@@ -83,7 +85,7 @@ def get_additional_memory_estimation_while_running_processes(
8385
extra_args = []
8486

8587
for _ in range(count):
86-
p = Process(target=target, args=[ready, measured, *extra_args])
88+
p = ctx.Process(target=target, args=[ready, measured, *extra_args])
8789
p.start()
8890
processes.append(p)
8991

@@ -129,7 +131,7 @@ def get_additional_memory_estimation_while_running_processes(
129131
f'{memory_estimation_difference_ratio=}'
130132
)
131133

132-
process = Process(target=parent_process)
134+
process = ctx.Process(target=parent_process)
133135
process.start()
134136
process.join()
135137

0 commit comments

Comments
 (0)