Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

2.4 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

/**
 * Counting semaphore that gates async work to at most `permits` in-flight
 * tasks. Waiters are released in FIFO order.
 */
export class Semaphore {
  private readonly maxPermits: number;
  private permits: number;
  private waiters: Array<() => void> = [];

  public constructor(permits: number) {
    if (!Number.isInteger(permits) || permits < 1) {
      throw new Error(
        `Semaphore permits must be a positive integer, got ${permits}`,
      );
    }
    this.maxPermits = permits;
    this.permits = permits;
  }

  /** Permits currently available. */
  public get available(): number {
    return this.permits;
  }

  /** Number of waiters queued for a permit. */
  public get pending(): number {
    return this.waiters.length;
  }

  public acquire(): Promise<void> {
    if (this.permits > 0) {
      this.permits--;
      return Promise.resolve();
    }
    return new Promise<void>((resolve) => {
      this.waiters.push(resolve);
    });
  }

  public release(): void {
    const next = this.waiters.shift();
    if (next) {
      next();
      return;
    }
    if (this.permits >= this.maxPermits) {
      throw new Error(
        "Semaphore.release() called more times than acquire() — refusing to exceed initial permit count",
      );
    }
    this.permits++;
  }

  /** Acquire a permit, run `fn`, then release the permit. */
  public async run<T>(fn: () => Promise<T>): Promise<T> {
    await this.acquire();
    try {
      return await fn();
    } finally {
      this.release();
    }
  }
}

/**
 * Map over `items` with bounded parallelism. Preserves input order in the
 * result. Rejects as soon as the first task rejects (like `Promise.all`);
 * already-started tasks keep running to completion in the background but
 * their results are dropped.
 */
// oxlint-disable-next-line marimo/prefer-object-params -- map-style helper, mirrors Array.prototype.map
export function mapWithConcurrency<T, R>(
  items: readonly T[],
  concurrency: number,
  fn: (item: T, index: number) => Promise<R>,
): Promise<R[]> {
  // Validate concurrency before the empty-input fast path so misconfiguration
  // is never silently accepted.
  const sem = new Semaphore(concurrency);
  if (items.length === 0) {
    return Promise.resolve([]);
  }
  return Promise.all(
    items.map((item, index) => sem.run(() => fn(item, index))),
  );
}