Skip to content

Commit f6c70c4

Browse files
tzarcdunk2k
andauthored
Allow for disabling of parallel processing of qmk find and qmk mass-compile. (qmk#22160)
Co-authored-by: Duncan Sutherland <[email protected]>
1 parent 81a3aa0 commit f6c70c4

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

lib/python/qmk/cli/find.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def find(cli):
2323
if len(cli.args.filter) == 0 and len(cli.args.print) > 0:
2424
cli.log.warning('No filters supplied -- keymaps not parsed, unable to print requested values.')
2525

26-
targets = search_keymap_targets(cli.args.keymap, cli.args.filter, cli.args.print)
26+
targets = search_keymap_targets([('all', cli.config.find.keymap)], cli.args.filter, cli.args.print)
2727
for keyboard, keymap, print_vals in targets:
2828
print(f'{keyboard}:{keymap}')
2929

lib/python/qmk/cli/mass_compile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ def mass_compile(cli):
9797
if len(cli.args.builds) > 0:
9898
targets = search_make_targets(cli.args.builds, cli.args.filter)
9999
else:
100-
targets = search_keymap_targets(cli.args.keymap, cli.args.filter)
100+
targets = search_keymap_targets([('all', cli.config.mass_compile.keymap)], cli.args.filter)
101101

102-
return mass_compile_targets(targets, cli.args.clean, cli.args.dry_run, cli.args.no_temp, cli.args.parallel, cli.args.env)
102+
return mass_compile_targets(targets, cli.args.clean, cli.args.dry_run, cli.config.mass_compile.no_temp, cli.config.mass_compile.parallel, cli.args.env)

lib/python/qmk/search.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import functools
55
import fnmatch
66
import logging
7-
import multiprocessing
87
import re
98
from typing import List, Tuple
109
from dotty_dict import dotty
1110
from milc import cli
1211

12+
from qmk.util import parallel_map
1313
from qmk.info import keymap_json
1414
import qmk.keyboard
1515
import qmk.keymap
@@ -78,17 +78,16 @@ def _expand_keymap_target(keyboard: str, keymap: str, all_keyboards: List[str] =
7878
all_keyboards = qmk.keyboard.list_keyboards()
7979

8080
if keyboard == 'all':
81-
with multiprocessing.Pool() as pool:
82-
if keymap == 'all':
83-
cli.log.info('Retrieving list of all keyboards and keymaps...')
84-
targets = []
85-
for kb in pool.imap_unordered(_all_keymaps, all_keyboards):
86-
targets.extend(kb)
87-
return targets
88-
else:
89-
cli.log.info(f'Retrieving list of keyboards with keymap "{keymap}"...')
90-
keyboard_filter = functools.partial(_keymap_exists, keymap=keymap)
91-
return [(kb, keymap) for kb in filter(lambda e: e is not None, pool.imap_unordered(keyboard_filter, all_keyboards))]
81+
if keymap == 'all':
82+
cli.log.info('Retrieving list of all keyboards and keymaps...')
83+
targets = []
84+
for kb in parallel_map(_all_keymaps, all_keyboards):
85+
targets.extend(kb)
86+
return targets
87+
else:
88+
cli.log.info(f'Retrieving list of keyboards with keymap "{keymap}"...')
89+
keyboard_filter = functools.partial(_keymap_exists, keymap=keymap)
90+
return [(kb, keymap) for kb in filter(lambda e: e is not None, parallel_map(keyboard_filter, all_keyboards))]
9291
else:
9392
if keymap == 'all':
9493
keyboard = qmk.keyboard.resolve_keyboard(keyboard)
@@ -117,8 +116,7 @@ def _filter_keymap_targets(target_list: List[Tuple[str, str]], filters: List[str
117116
targets = [(kb, km, {}) for kb, km in target_list]
118117
else:
119118
cli.log.info('Parsing data for all matching keyboard/keymap combinations...')
120-
with multiprocessing.Pool() as pool:
121-
valid_keymaps = [(e[0], e[1], dotty(e[2])) for e in pool.imap_unordered(_load_keymap_info, target_list)]
119+
valid_keymaps = [(e[0], e[1], dotty(e[2])) for e in parallel_map(_load_keymap_info, target_list)]
122120

123121
function_re = re.compile(r'^(?P<function>[a-zA-Z]+)\((?P<key>[a-zA-Z0-9_\.]+)(,\s*(?P<value>[^#]+))?\)$')
124122
equals_re = re.compile(r'^(?P<key>[a-zA-Z0-9_\.]+)\s*=\s*(?P<value>[^#]+)$')
@@ -179,10 +177,10 @@ def f(e):
179177
return targets
180178

181179

182-
def search_keymap_targets(keymap='default', filters: List[str] = [], print_vals: List[str] = []) -> List[Tuple[str, str, List[Tuple[str, str]]]]:
180+
def search_keymap_targets(targets: List[Tuple[str, str]] = [('all', 'default')], filters: List[str] = [], print_vals: List[str] = []) -> List[Tuple[str, str, List[Tuple[str, str]]]]:
183181
"""Search for build targets matching the supplied criteria.
184182
"""
185-
return list(sorted(_filter_keymap_targets(expand_keymap_targets([('all', keymap)]), filters, print_vals), key=lambda e: (e[0], e[1])))
183+
return list(sorted(_filter_keymap_targets(expand_keymap_targets(targets), filters, print_vals), key=lambda e: (e[0], e[1])))
186184

187185

188186
def search_make_targets(targets: List[str], filters: List[str] = [], print_vals: List[str] = []) -> List[Tuple[str, str, List[Tuple[str, str]]]]:

lib/python/qmk/util.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Utility functions.
2+
"""
3+
import contextlib
4+
import multiprocessing
5+
6+
from milc import cli
7+
8+
9+
@contextlib.contextmanager
10+
def parallelize():
11+
"""Returns a function that can be used in place of a map() call.
12+
13+
Attempts to use `mpire`, falling back to `multiprocessing` if it's not
14+
available. If parallelization is not requested, returns the original map()
15+
function.
16+
"""
17+
18+
# Work out if we've already got a config value for parallel searching
19+
if cli.config.user.parallel_search is None:
20+
parallel_search = True
21+
else:
22+
parallel_search = cli.config.user.parallel_search
23+
24+
# Non-parallel searches use `map()`
25+
if not parallel_search:
26+
yield map
27+
return
28+
29+
# Prefer mpire's `WorkerPool` if it's available
30+
with contextlib.suppress(ImportError):
31+
from mpire import WorkerPool
32+
from mpire.utils import make_single_arguments
33+
with WorkerPool() as pool:
34+
35+
def _worker(func, *args):
36+
# Ensure we don't unpack tuples -- mpire's `WorkerPool` tries to do so normally so we tell it not to.
37+
for r in pool.imap_unordered(func, make_single_arguments(*args, generator=False), progress_bar=True):
38+
yield r
39+
40+
yield _worker
41+
return
42+
43+
# Otherwise fall back to multiprocessing's `Pool`
44+
with multiprocessing.Pool() as pool:
45+
yield pool.imap_unordered
46+
47+
48+
def parallel_map(*args, **kwargs):
49+
"""Effectively runs `map()` but executes it in parallel if necessary.
50+
"""
51+
with parallelize() as map_fn:
52+
# This needs to be enclosed in a `list()` as some implementations return
53+
# a generator function, which means the scope of the pool is closed off
54+
# before the results are returned. Returning a list ensures results are
55+
# materialised before any worker pool is shut down.
56+
return list(map_fn(*args, **kwargs))

0 commit comments

Comments
 (0)