mcollina/fastq hiện có 1.1k sao trên GitHub, viết chủ yếu bằng JavaScript. Fast, in memory work queue
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
If you need zero-overhead series function call, check out
fastseries. For zero-overhead parallel
function call, check out fastparallel.
Installation
Usage
API
Licence & copyright
Install
npm i fastq --save
Usage (callback API)
'use strict'
const queue = require('fastq')(worker, 1)
queue.push(42, function (err, result) {
if (err) { throw err }
console.log('the result is', result)
})
function worker (arg, cb) {
cb(null, arg * 2)
}
Usage (promise API)
const queue = require('fastq').promise(worker, 1)
async function worker (arg) {
return arg * 2
}
async function run () {
const result = await queue.push(42)
console.log('the result is', result)
}
run()
Setting "this"
'use strict'
const that = { hello: 'world' }
const queue = require('fastq')(that, worker, 1)
queue.push(42, function (err, result) {
if (err) { throw err }
console.log(this)
console.log('the result is', result)
})
function worker (arg, cb) {
console.log(this)
cb(null, arg * 2)
}
Async context and AsyncLocalStorage
fastq does not propagate the async context of the caller of push() or
unshift() to the worker. A task that has to wait in the queue is
started from the completion of the task that came before it, so inside
the worker AsyncLocalStorage.getStore() (and any other async-context
tracking) may return the store of a previous, unrelated task.
Do not rely on ambient async context inside a worker for anything that
matters, especially authentication or authorization data like the
current user, tenant, or role. Pass that data explicitly as part of the
task instead:
queue.push({ user: req.user, id: req.params.id })
If you need the worker to run in the context of the caller, capture it
when pushing with
AsyncLocalStorage.snapshot()
and run the work inside it:
'use strict'
const { AsyncLocalStorage } = require('node:async_hooks')
const queue = require('fastq').promise(worker, 1)
const als = new AsyncLocalStorage()
async function worker ({ run, value }) {
return run(async () => {
// als.getStore() returns the store of the caller of push()
return doWork(value)
})
}
als.run({ user: 'alice' }, () => {
queue.push({ run: AsyncLocalStorage.snapshot(), value: 42 })
})
The same approach works with the callback API: call run() within the
worker and invoke the callback from inside it.
Using with TypeScript (callback API)
'use strict'
import * as fastq from "fastq";
import type { queue, done } from "fastq";
type Task = {
id: number
}
const q: queue<Task> = fastq(worker, 1)
q.push({ id: 42})
function worker (arg: Task, cb: done) {
console.log(arg.id)
cb(null)
}
Using with TypeScript (promise API)
'use strict'
import * as fastq from "fastq";
import type { queueAsPromised } from "fastq";
type Task = {
id: number
}
const q: queueAsPromised<Task> = fastq.promise(asyncWorker, 1)
q.push({ id: 42}).catch((err) => console.error(err))
async function asyncWorker (arg: Task): Promise<void> {
// No need for a try-catch block, fastq handles errors automatically
console.log(arg.id)
}
API
fastqueue()
queue#push()
queue#unshift()
queue#pause()
queue#resume()
queue#idle()
queue#length()
queue#getQueue()
queue#kill()
queue#killAndDrain()
queue#error()
queue#concurrency
queue#drain
queue#empty
queue#saturated
fastqueue.promise()
fastqueue([that], worker, concurrency)
Creates a new queue.
Arguments:
that, optional context of the worker function.
worker, worker function, it would be called with that as this,
if that is specified.
concurrency, number of concurrent tasks that could be executed in
parallel.
queue.push(task, done)
Add a task at the end of the queue. done(err, result) will be called
when the task was processed.
queue.unshift(task, done)
Add a task at the beginning of the queue. done(err, result) will be called
when the task was processed.
queue.pause()
Pause the processing of tasks. Currently worked tasks are not
stopped.
queue.resume()
Resume the processing of tasks.
queue.idle()
Returns false if there are tasks being processed or waiting to be processed.
true otherwise.
queue.length()
Returns the number of tasks waiting to be processed (in the queue).
queue.getQueue()
Returns all the tasks be processed (in the queue). Returns empty array when there are no tasks
queue.kill()
Removes all tasks waiting to be processed, and reset drain to an empty
function.
queue.killAndDrain()
Same than kill but the drain function will be called before reset to empty.
queue.error(handler)
Set a global error handler. handler(err, task) will be called
each time a task is completed, err will be not null if the task has thrown an error.
queue.concurrency
Property that returns the number of concurrent tasks that could be executed in
parallel. It can be altered at runtime.
queue.paused
Property (Read-Only) that returns true when the queue is in a paused state.
queue.drain
Function that will be called when the last
item from the queue has been processed by a worker.
It can be altered at runtime.
queue.empty
Function that will be called when the last
item from the queue has been assigned to a worker.
It can be altered at runtime.
queue.saturated
Function that will be called when the queue hits the concurrency
limit.
It can be altered at runtime.
Creates a new queue with Promise apis. It also offers all the methods
and properties of the object returned by fastqueue with the modified
push and unshift methods.
Node v10+ is required to use the promisified version.
Arguments:
that, optional context of the worker function.
worker, worker function, it would be called with that as this,
if that is specified. It MUST return a Promise.
concurrency, number of concurrent tasks that could be executed in
parallel.
queue.push(task) => Promise
Add a task at the end of the queue. The returned Promise will be fulfilled (rejected)
when the task is completed successfully (unsuccessfully).
This promise could be ignored as it will not lead to a 'unhandledRejection'.
queue.unshift(task) => Promise
Add a task at the beginning of the queue. The returned Promise will be fulfilled (rejected)
when the task is completed successfully (unsuccessfully).
This promise could be ignored as it will not lead to a 'unhandledRejection'.
queue.drained() => Promise
Wait for the queue to be drained. The returned Promise will be resolved when all tasks in the queue have been processed by a worker.
This promise could be ignored as it will not lead to a 'unhandledRejection'.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/mcollina/fastq là nguồn chính thức.
mcollina/fastq có bao nhiêu sao?
mcollina/fastq có 1.1k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/mcollina/fastq. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
mcollina/fastq có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho mcollina/fastq. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
mcollina/fastq còn đang phát triển không?
Commit gần nhất trên mcollina/fastq là 14 ngày trước (theo timestamp GitHub). Repo có 66 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
mcollina/fastq viết bằng ngôn ngữ gì?
mcollina/fastq chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về fastq?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về fastq.