Skip to content

Commit

Permalink
[python] add task pool for regeneration (#6214)
Browse files Browse the repository at this point in the history
  • Loading branch information
msyyc authored Mar 4, 2025
1 parent 7c5aabe commit 41efd5f
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: internal
packages:
- "@typespec/http-client-python"
---

add task pool for regeneration
31 changes: 28 additions & 3 deletions packages/http-client-python/eng/scripts/ci/regenerate.ts
Original file line number Diff line number Diff line change
@@ -316,6 +316,20 @@ function _getCmdList(spec: string, flags: RegenerateFlags): TspCommand[] {
});
}

async function runTaskPool(tasks: Array<() => Promise<void>>, poolLimit: number): Promise<void> {
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<void> {
if (flags.flavor === undefined) {
await regenerate({ flavor: "azure", ...flags });
@@ -331,11 +345,22 @@ async function regenerate(flags: RegenerateFlagsInput): Promise<void> {
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<void>> = cmdList.map((tspCommand) => {
return () => executeCommand(tspCommand);
});

// Run tasks with a concurrency limit
await runTaskPool(tasks, 30);
}
}

const start = performance.now();
regenerate(argv.values)
.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}`));

0 comments on commit 41efd5f

Please sign in to comment.