Skip to content

Commit 270b4e9

Browse files
committed
Add option to limit the number of mp workers
Due to python/cpython#89240, any use of mp.Pool with a number of workers greater than 60 fails. This means that by using cpu_count(), any system with more than 60 logical cores will crash when attempting to run. Solve this by adding a flag to allow limiting the number of workers for users with systems with that many cores
1 parent 96e25a1 commit 270b4e9

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

gersemi/__main__.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
from dataclasses import fields
3+
import multiprocessing as mp
34
import pathlib
45
import sys
56
from lark import __version__ as lark_version
@@ -144,6 +145,15 @@ def create_argparser():
144145
""",
145146
)
146147

148+
parser.add_argument(
149+
"-w",
150+
"--workers",
151+
dest="workers",
152+
type=int,
153+
default=mp.cpu_count(),
154+
help = "number of workers to run (default is number of cores)"
155+
)
156+
147157
return parser
148158

149159

@@ -170,8 +180,9 @@ def main():
170180

171181
configuration = make_configuration(args)
172182
mode = get_mode(args)
183+
num_workers = args.workers
173184

174-
sys.exit(run(mode, configuration, args.sources))
185+
sys.exit(run(mode, configuration, num_workers, args.sources))
175186

176187

177188
if __name__ == "__main__":

gersemi/runner.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ def consume_task_result(task_result: TaskResult) -> Tuple[Path, int]:
137137
return path, return_code
138138

139139

140-
def create_pool(is_stdin_in_sources):
140+
def create_pool(is_stdin_in_sources, num_workers):
141141
if is_stdin_in_sources:
142142
return mp_dummy.Pool
143-
return partial(mp.Pool, processes=mp.cpu_count())
143+
return partial(mp.Pool, processes=num_workers)
144144

145145

146146
def filter_already_formatted_files(
@@ -163,12 +163,12 @@ def store_files_in_cache(
163163
cache.store_files(configuration_summary, files)
164164

165165

166-
def run(mode: Mode, configuration: Configuration, sources: Iterable[Path]):
166+
def run(mode: Mode, configuration: Configuration, num_workers, sources: Iterable[Path]):
167167
configuration_summary = configuration.summary()
168168
requested_files = get_files(sources)
169169
task = select_task(mode, configuration)
170170

171-
pool_cm = create_pool(Path("-") in requested_files)
171+
pool_cm = create_pool(Path("-") in requested_files, num_workers)
172172
with create_cache() as cache, pool_cm() as pool:
173173
files_to_format = list(
174174
filter_already_formatted_files(

0 commit comments

Comments
 (0)