vercel / async-sema Goto Github PK
View Code? Open in Web Editor NEWSemaphore using `async` and `await`
Home Page: https://npmjs.com/async-sema
License: MIT License
Semaphore using `async` and `await`
Home Page: https://npmjs.com/async-sema
License: MIT License
async function f() {
const lim = RateLimit(0.5, { timeUnit: 1000 }); // rps
console.time('order');
for (let i = 0; i < 100; i++) {
await lim();
console.timeLog('order');
}
console.timeEnd('order');
}
f();
order: 1.083s
order: 2.085s
order: 3.087s
order: 4.091s
async function f() {
const lim = RateLimit(1, { timeUnit: 2000 }); // rps
console.time('order');
for (let i = 0; i < 100; i++) {
await lim();
console.timeLog('order');
}
console.timeEnd('order');
}
f();
order: 2.071s
order: 4.075s
order: 6.078s
order: 8.081s
it should work with number less than 1 or maybe we should write it in the doc
I am using the simple implementation RateLimiter and I am using the lowest value of 1 (1 request / sec) but I still get 500 errors from the server which I am calling. so I wonder if its possible to further reduce the number of calls say 1 every 2 seconds?
I find I have the need to do a series of concurrent tasks, with a max concurrency, but also with a max total run time. After this time, whatever hasn't finished or started can be ignored.
I propose to add a new API
sema.cancel()
Which rejects all pending .acquire()
calls. This way I can do the following
const timeout = setTimeout(() => sema.cancel(), 5000);
await Promise.all(jobs.map(async job => {
try {
sema.acquire();
} catch (err) {
if (err.code === 'CANCELLED') {
return null
}
throw err
}
try {
await doWork(job);
} finally {
sema.release();
}
}));
clearTimeout(timeout);
If accepted I can make a PR
All examples are currently in an examples
directory, but we should perhaps include the basic on on the README itself
Thanks for this code!
const lim = RateLimit(2, {
timeUnit: (5000),
});
await Promise.all(
images.map(async (image, index) => {
await lim();
const parsed = await _prepImage.call(this, image);
await util.downloadImage(parsed.url, parsed.target);
})
);
From what I can tell, the above code downloads two images at a time and pauses 5 seconds between concurrent downloads.
I am noticing though, the last image download waits when I would like it to exit as soon as it's resolved.
In other words, since there are no more images to download (as it it the last promise to resolve) I don't want it to wait.
RateLimit
properly (or, is this a good use case for RateLimit)?Many thanks in advance for the help and for sharing this code!
When adding this to a project, my app no longer loads in IE 11. I can't find any polyfills which alleviate the problem.
Has anyone had success running in IE?
🚨 You need to enable Continuous Integration on all branches of this repository. 🚨
To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.
Since we didn’t receive a CI status on the greenkeeper/initial
branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.
If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/
.
Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial
branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.
I'm trying to limit uploads to 10 simultaneously, and I'm doing it like this:
const uploadSema = new Sema(10)
for (const upload of uploads) {
await uploadSema.acquire()
uploadOne(upload).finally(() => uploadSema.release())
}
await uploadSema.drain()
It's reasonably nice, but I was wondering if there wasn't a way to make this nicer.
I moved the logic to this helper function
export const queuedWork = async (items, fn, workers = 10) => {
const sema = new Sema(workers)
let threw = null
for (const item of items) {
if (threw) break
await sema.acquire()
// eslint-disable-next-line promise/catch-or-return
Promise.resolve(fn(item))
.catch(err => {
threw = err
})
.finally(() => sema.release())
}
await sema.drain()
if (threw) throw threw
}
Is this a good way of going about it? Is there maybe a more elegant way?
(wrote these tests too)
test('queuedWork async', async () => {
const out = []
await queuedWork([1, 5, 7, 89, 2], async n => out.push(n), 2)
expect(out).toEqual([1, 5, 7, 89, 2])
})
test('queuedWork sync', async () => {
const out = []
await queuedWork([1, 5, 7, 89, 2], n => out.push(n), 2)
expect(out).toEqual([1, 5, 7, 89, 2])
})
test('queuedWork throws', async () => {
const out = []
await expect(
queuedWork(
[1, 2, 3, 4, 5],
async n => {
if (n === 2) throw new Error('meep')
else out.push(n)
},
2
)
).rejects.toThrow('meep')
expect(out).toEqual([1, 3])
})
test('queuedWork throws sync', async () => {
const out = []
await expect(
queuedWork(
[1, 2, 3, 4, 5],
n => {
if (n === 2) throw new Error('meep')
else out.push(n)
},
2
)
).rejects.toThrow('meep')
expect(out).toEqual([1])
})
How should one best figure out how to meaningfully determine this value? The examples seem to not follow any strict pattern.
Can the readme be made to include a rough guideline or set of heuristics to determine this for a given type of application?
Hi all, currently typescript definition seems broken, typescript compiler complains about malformed type definition file (.d.ts
) syntax.
maybe it's off topic, I love this package. so i've migrated async-sema package to typescript. i'd like to discuss about publishing typescript version of async-sema. should i publish typescript version of async-sema package as individual package? or just send PR to this repo? Please kindly give me your advice. Thanks :)
Looking at the source code and the examples, it seems to me that the library is using p()
to signal/free up a resource, whereas v()
is used to wait/"block" on the semaphore.
However, according to the definition of a semaphore, the uses of p()
and v()
are the other way around. Some resources to verify are:
Please let me know if I'm misunderstanding the use case here. :)
Now the constructor requires an on empty object for default args:
new Sema(10, {})
while
new Sema(10)
would be optimal.
Whenever I import Sema or RateLimit.
i.e import { RateLimit, Sema } from 'async-sema';
I'm getting the following error:
Uncaught TypeError: Class extends value #<Object> is not a constructor or null
Is there any workaround for it?
Thanks.
sorry to interrupt. what does nr
stands for, I thought it was num of resource
, but then what is the capacity
variable?
The nr
value given to Sema() does not stop .release()
from being called as many times as a user wants.
Example below shows an nr value of 1, but the user is able to obtain 2 concurrent uses.
const Sema = require("async-sema").Sema;
let i = 0;
const a = new Sema(1);
a.release();
a.acquire().then(() => {
i++;
console.log(`i = ${i}`);
return a.acquire();
}).then(() => {
i++;
console.log(`i = ${i}`);
return a.acquire();
});
I think adding the following utility function might simplify working with semaphores a lot
export class Sema {
public runTask<T>(task: (token?: any) => Promise<T>): Promise<T> {
return this.acquire().then(token =>
Promise.resolve(token)
.then(task)
.finally(() => sem.release(token))
);
}
}
It would allow to simply and safely queue tasks for processing while making sure that acquired tokens are never lost:
const sem = new Sema(3);
// Run many task in parallel limited by sem
for (let i = 0; i < 100; i++) {
sem.runTask(async () => {
// do some stuff
});
}
// Alternatively use Promise.all and map
await Promise.all(items.map(item => sem.runTask(() => processItem(item))))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.