Skip to content

Commit a13d2fb

Browse files
wip: using threads to build command table
1 parent 0873e58 commit a13d2fb

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"program": "${workspaceRoot}/src/azure-cli/azure/cli/__main__.py",
2424
"cwd": "${workspaceRoot}",
2525
"args": [
26-
"--version"
26+
"Version"
2727
],
2828
"console": "externalTerminal",
2929
},

src/azure-cli-core/azure/cli/core/__init__.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import sys
1111
import timeit
12+
from concurrent.futures import ThreadPoolExecutor
1213

1314
from knack.cli import CLI
1415
from knack.commands import CLICommandsLoader
@@ -258,31 +259,48 @@ def _update_command_table_from_modules(args, command_modules=None):
258259
logger.debug("Loading command modules:")
259260
logger.debug(self.header_mod)
260261

261-
for mod in [m for m in command_modules if m not in BLOCKED_MODS]:
262+
print(f"*** Starting SUT***")
263+
start_time_wrapper = timeit.default_timer()
264+
265+
def load_module_threaded(mod):
262266
try:
263267
start_time = timeit.default_timer()
264268
module_command_table, module_group_table = _load_module_command_loader(self, args, mod)
265269
import_module_breaking_changes(mod)
266-
for cmd in module_command_table.values():
267-
cmd.command_source = mod
268-
self.command_table.update(module_command_table)
269-
self.command_group_table.update(module_group_table)
270-
271270
elapsed_time = timeit.default_timer() - start_time
272-
logger.debug(self.item_format_string, mod, elapsed_time,
273-
len(module_group_table), len(module_command_table))
274-
count += 1
275-
cumulative_elapsed_time += elapsed_time
276-
cumulative_group_count += len(module_group_table)
277-
cumulative_command_count += len(module_command_table)
271+
return (mod, module_command_table, module_group_table, elapsed_time, None)
278272
except Exception as ex: # pylint: disable=broad-except
279-
# Changing this error message requires updating CI script that checks for failed
280-
# module loading.
281-
from azure.cli.core import telemetry
282-
logger.error("Error loading command module '%s': %s", mod, ex)
283-
telemetry.set_exception(exception=ex, fault_type='module-load-error-' + mod,
284-
summary='Error loading module: {}'.format(mod))
285-
logger.debug(traceback.format_exc())
273+
return (mod, {}, {}, 0, ex)
274+
275+
# Use ThreadPoolExecutor to load modules in parallel
276+
with ThreadPoolExecutor(max_workers=4) as executor:
277+
futures = [executor.submit(load_module_threaded, mod)
278+
for mod in command_modules if mod not in BLOCKED_MODS]
279+
280+
for future in futures:
281+
mod, module_command_table, module_group_table, elapsed_time, error = future.result()
282+
if error:
283+
# Changing this error message requires updating CI script that checks for failed
284+
# module loading.
285+
from azure.cli.core import telemetry
286+
logger.error("Error loading command module '%s': %s", mod, error)
287+
telemetry.set_exception(exception=error, fault_type='module-load-error-' + mod,
288+
summary='Error loading module: {}'.format(mod))
289+
logger.debug(traceback.format_exc())
290+
else:
291+
for cmd in module_command_table.values():
292+
cmd.command_source = mod
293+
self.command_table.update(module_command_table)
294+
self.command_group_table.update(module_group_table)
295+
296+
logger.debug(self.item_format_string, mod, elapsed_time,
297+
len(module_group_table), len(module_command_table))
298+
count += 1
299+
cumulative_elapsed_time += elapsed_time
300+
cumulative_group_count += len(module_group_table)
301+
cumulative_command_count += len(module_command_table)
302+
elapsed_time = timeit.default_timer() - start_time_wrapper
303+
print(f"*** SUT operation *** took: {elapsed_time:.6f} seconds for {len(command_modules)} modules")
286304
# Summary line
287305
logger.debug(self.item_format_string,
288306
"Total ({})".format(count), cumulative_elapsed_time,

src/azure-cli-core/azure/cli/core/commands/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ def execute(self, args):
517517
args = _pre_command_table_create(self.cli_ctx, args)
518518

519519
self.cli_ctx.raise_event(EVENT_INVOKER_PRE_CMD_TBL_CREATE, args=args)
520+
# @NOTE: below takes forever:
520521
self.commands_loader.load_command_table(args)
521522
self.cli_ctx.raise_event(EVENT_INVOKER_PRE_CMD_TBL_TRUNCATE,
522523
load_cmd_tbl_func=self.commands_loader.load_command_table, args=args)

0 commit comments

Comments
 (0)