diff --git a/.chronus/changes/fix-nightly-2025-2-3-17-27-31.md b/.chronus/changes/fix-nightly-2025-2-3-17-27-31.md new file mode 100644 index 00000000000..e02a792fc22 --- /dev/null +++ b/.chronus/changes/fix-nightly-2025-2-3-17-27-31.md @@ -0,0 +1,7 @@ +--- +changeKind: internal +packages: + - "@azure-tools/typespec-python" +--- + +add task pool for regeneration \ No newline at end of file diff --git a/packages/typespec-python/scripts/eng/regenerate.ts b/packages/typespec-python/scripts/eng/regenerate.ts index 6c872705fd2..677578c9139 100644 --- a/packages/typespec-python/scripts/eng/regenerate.ts +++ b/packages/typespec-python/scripts/eng/regenerate.ts @@ -281,6 +281,20 @@ function _getCmdList(spec: string, flags: RegenerateFlags): TspCommand[] { }); } +async function runTaskPool(tasks: Array<() => Promise>, poolLimit: number): Promise { + let currentIndex = 0; + + async function worker() { + while (currentIndex < tasks.length) { + const index = currentIndex++; + await tasks[index](); + } + } + + const workers = new Array(Math.min(poolLimit, tasks.length)).fill(null).map(() => worker()); + await Promise.all(workers); +} + async function regenerate(flags: RegenerateFlagsInput): Promise { if (flags.flavor === undefined) { await regenerate({ ...flags, flavor: "azure" }); @@ -296,8 +310,14 @@ async function regenerate(flags: RegenerateFlagsInput): Promise { const cmdList: TspCommand[] = subdirectories.flatMap((subdirectory) => _getCmdList(subdirectory, flagsResolved), ); - const PromiseCommands = cmdList.map((tspCommand) => executeCommand(tspCommand)); - await Promise.all(PromiseCommands); + + // Create tasks as functions for the pool + const tasks: Array<() => Promise> = cmdList.map((tspCommand) => { + return () => executeCommand(tspCommand); + }); + + // Run tasks with a concurrency limit + await runTaskPool(tasks, 30); } } @@ -319,6 +339,7 @@ const argv = yargs(hideBin(process.argv)) description: "Specify filename if you only want to generate a subset", }).argv; +const start = performance.now(); regenerate(argv as RegenerateFlags) - .then(() => console.log("Regeneration successful")) + .then(() => console.log(`Regeneration successful, time taken: ${Math.round((performance.now() - start) / 1000)} s`)) .catch((error) => console.error(`Regeneration failed: ${error.message}`));