Skip to content

Commit 41efd5f

Browse files
authored
[python] add task pool for regeneration (#6214)
same change with Azure/autorest.python#3049
1 parent 7c5aabe commit 41efd5f

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
3+
changeKind: internal
4+
packages:
5+
- "@typespec/http-client-python"
6+
---
7+
8+
add task pool for regeneration

packages/http-client-python/eng/scripts/ci/regenerate.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ function _getCmdList(spec: string, flags: RegenerateFlags): TspCommand[] {
316316
});
317317
}
318318

319+
async function runTaskPool(tasks: Array<() => Promise<void>>, poolLimit: number): Promise<void> {
320+
let currentIndex = 0;
321+
322+
async function worker() {
323+
while (currentIndex < tasks.length) {
324+
const index = currentIndex++;
325+
await tasks[index]();
326+
}
327+
}
328+
329+
const workers = new Array(Math.min(poolLimit, tasks.length)).fill(null).map(() => worker());
330+
await Promise.all(workers);
331+
}
332+
319333
async function regenerate(flags: RegenerateFlagsInput): Promise<void> {
320334
if (flags.flavor === undefined) {
321335
await regenerate({ flavor: "azure", ...flags });
@@ -331,11 +345,22 @@ async function regenerate(flags: RegenerateFlagsInput): Promise<void> {
331345
const cmdList: TspCommand[] = subdirectories.flatMap((subdirectory) =>
332346
_getCmdList(subdirectory, flagsResolved),
333347
);
334-
const PromiseCommands = cmdList.map((tspCommand) => executeCommand(tspCommand));
335-
await Promise.all(PromiseCommands);
348+
349+
// Create tasks as functions for the pool
350+
const tasks: Array<() => Promise<void>> = cmdList.map((tspCommand) => {
351+
return () => executeCommand(tspCommand);
352+
});
353+
354+
// Run tasks with a concurrency limit
355+
await runTaskPool(tasks, 30);
336356
}
337357
}
338358

359+
const start = performance.now();
339360
regenerate(argv.values)
340-
.then(() => console.log("Regeneration successful"))
361+
.then(() =>
362+
console.log(
363+
`Regeneration successful, time taken: ${Math.round((performance.now() - start) / 1000)} s`,
364+
),
365+
)
341366
.catch((error) => console.error(`Regeneration failed: ${error.message}`));

0 commit comments

Comments
 (0)