Permalink
May 4, 2020 18:50
Newer
100644
200 lines (145 sloc)
5.36 KB
Ignoring revisions in .git-blame-ignore-revs.
1
# fastq
2
3
![ci][ci-url]
4
[![npm version][npm-badge]][npm-url]
5
[![Dependency Status][david-badge]][david-url]
6
7
Fast, in memory work queue. `fastq` is API compatible with
8
[`async.queue`](https://github.com/caolan/async#queueworker-concurrency)
9
10
Benchmarks (1 million tasks):
11
12
* setImmediate: 812ms
13
* fastq: 854ms
14
* async.queue: 1298ms
15
* neoAsync.queue: 1249ms
16
17
Obtained on node 12.16.1, on a dedicated server.
18
19
If you need zero-overhead series function call, check out
20
[fastseries](http://npm.im/fastseries). For zero-overhead parallel
21
function call, check out [fastparallel](http://npm.im/fastparallel).
22
23
[](https://github.com/feross/standard)
24
25
* <a href="#install">Installation</a>
26
* <a href="#usage">Usage</a>
27
* <a href="#api">API</a>
28
* <a href="#license">Licence & copyright</a>
29
30
## Install
31
32
`npm i fastq --save`
33
34
## Usage
35
36
```js
37
'use strict'
38
39
var queue = require('fastq')(worker, 1)
40
41
queue.push(42, function (err, result) {
42
if (err) { throw err }
43
console.log('the result is', result)
44
})
45
46
function worker (arg, cb) {
47
cb(null, 42 * 2)
48
}
49
```
50
51
### Setting this
52
53
```js
54
'use strict'
55
56
var that = { hello: 'world' }
57
var queue = require('fastq')(that, worker, 1)
58
59
queue.push(42, function (err, result) {
60
if (err) { throw err }
61
console.log(this)
62
console.log('the result is', result)
63
})
64
65
function worker (arg, cb) {
66
console.log(this)
67
cb(null, 42 * 2)
68
}
69
```
70
71
## API
72
73
* <a href="#fastqueue"><code>fastqueue()</code></a>
74
* <a href="#push"><code>queue#<b>push()</b></code></a>
75
* <a href="#unshift"><code>queue#<b>unshift()</b></code></a>
76
* <a href="#pause"><code>queue#<b>pause()</b></code></a>
77
* <a href="#resume"><code>queue#<b>resume()</b></code></a>
78
* <a href="#idle"><code>queue#<b>idle()</b></code></a>
79
* <a href="#length"><code>queue#<b>length()</b></code></a>
80
* <a href="#getQueue"><code>queue#<b>getQueue()</b></code></a>
81
* <a href="#kill"><code>queue#<b>kill()</b></code></a>
82
* <a href="#killAndDrain"><code>queue#<b>killAndDrain()</b></code></a>
83
* <a href="#concurrency"><code>queue#<b>concurrency</b></code></a>
84
* <a href="#drain"><code>queue#<b>drain</b></code></a>
85
* <a href="#empty"><code>queue#<b>empty</b></code></a>
86
* <a href="#saturated"><code>queue#<b>saturated</b></code></a>
87
88
-------------------------------------------------------
89
<a name="fastqueue"></a>
90
### fastqueue([that], worker, concurrency)
91
92
Creates a new queue.
93
94
Arguments:
95
96
* `that`, optional context of the `worker` function.
97
* `worker`, worker function, it would be called with `that` as `this`,
98
if that is specified.
99
* `concurrency`, number of concurrent tasks that could be executed in
100
parallel.
101
102
-------------------------------------------------------
103
<a name="push"></a>
104
### queue.push(task, done)
105
106
Add a task at the end of the queue. `done(err, result)` will be called
107
when the task was processed.
108
109
-------------------------------------------------------
110
<a name="unshift"></a>
111
### queue.unshift(task, done)
112
113
Add a task at the beginning of the queue. `done(err, result)` will be called
114
when the task was processed.
115
116
-------------------------------------------------------
117
<a name="pause"></a>
118
### queue.pause()
119
120
Pause the processing of tasks. Currently worked tasks are not
121
stopped.
122
123
-------------------------------------------------------
124
<a name="resume"></a>
125
### queue.resume()
126
127
Resume the processing of tasks.
128
129
-------------------------------------------------------
130
<a name="idle"></a>
131
### queue.idle()
132
133
Returns `false` if there are tasks being processed or waiting to be processed.
134
`true` otherwise.
135
136
-------------------------------------------------------
137
<a name="length"></a>
138
### queue.length()
139
140
Returns the number of tasks waiting to be processed (in the queue).
141
142
-------------------------------------------------------
143
<a name="getQueue"></a>
144
### queue.getQueue()
145
146
Returns all the tasks be processed (in the queue). Returns empty array when there are no tasks
147
148
-------------------------------------------------------
149
<a name="kill"></a>
150
### queue.kill()
151
152
Removes all tasks waiting to be processed, and reset `drain` to an empty
153
function.
154
155
-------------------------------------------------------
156
<a name="killAndDrain"></a>
157
### queue.killAndDrain()
158
159
Same than `kill` but the `drain` function will be called before reset to empty.
160
161
-------------------------------------------------------
162
<a name="concurrency"></a>
163
### queue.concurrency
164
165
Property that returns the number of concurrent tasks that could be executed in
166
parallel. It can be altered at runtime.
167
168
-------------------------------------------------------
169
<a name="drain"></a>
170
### queue.drain
171
172
Function that will be called when the last
173
item from the queue has been processed by a worker.
174
It can be altered at runtime.
175
176
-------------------------------------------------------
177
<a name="empty"></a>
178
### queue.empty
179
180
Function that will be called when the last
181
item from the queue has been assigned to a worker.
182
It can be altered at runtime.
183
184
-------------------------------------------------------
185
<a name="saturated"></a>
186
### queue.saturated
187
188
Function that will be called when the queue hits the concurrency
189
limit.
190
It can be altered at runtime.
191
192
## License
193
194
ISC
195
196
[ci-url]: https://github.com/mcollina/fastq/workflows/ci/badge.svg
197
[npm-badge]: https://badge.fury.io/js/fastq.svg
198
[npm-url]: https://badge.fury.io/js/fastq
199
[david-badge]: https://david-dm.org/mcollina/fastq.svg
200
[david-url]: https://david-dm.org/mcollina/fastq