Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: I feel I have a use-case that I'm not sure if the queue fulfils when reading the doc #204

Open
maelp opened this issue Jan 13, 2024 · 0 comments

Comments

@maelp
Copy link

maelp commented Jan 13, 2024

I want to have a pool of workers completing multiple jobs with rate-limiting / concurrency, but because there are a lot of jobs, and each job description can take up a big chunk of memory, I would like to only create the job "on-the-fly" as soon as a worker slot is ready. Is that easy to do with p-queue ?

For instance a "solution" like that Promise.all(myHugeListOfJobs) would not work because it would require creating all the tasks at once and fill up the memory.

For now I use async-sema with a semaphore, and a util like this:

import { Sema } from "async-sema";

export const runTasksWithConcurrency = async (
  taskGenerator: AsyncGenerator<() => Promise<any>, void, unknown>,
  concurrency: number
): Promise<PromiseSettledResult<any>[]> => {
  const semaphore = new Sema(concurrency);
  const tasks: Promise<any>[] = [];

  for await (const task of taskGenerator) {
    await semaphore.acquire();
    tasks.push(
      (async () => {
        try {
          return await task();
        } finally {
          semaphore.release();
        }
      })()
    );
  }

  return Promise.allSettled(tasks);
};

that I use like this

  async function* taskGenerator() {
    for (const data of myHugeListOfItemsGenerator) {
      yield async () => {
          await doSomeWork();
      };
    }
  }

  const runResults = await runTasksWithConcurrency(taskGenerator(), 50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant