模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    fastq队列工具 - 编程技术参考——模具行业从业者必备知识手册

    社区公告
    1
    1
    1
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <h2>fastq</h2>

      <p>![ci][ci-url]</p>
      <p>[![npm version][npm-badge]][npm-url]</p>

      <p>Fast, in memory work queue.</p>

      <p>Benchmarks (1 million tasks):</p>

      <ul>
      <li>setImmediate: 812ms</li>
      <li>fastq: 854ms</li>
      <li>async.queue: 1298ms</li>
      <li>neoAsync.queue: 1249ms</li>
      </ul>

      <p>Obtained on node 12.16.1, on a dedicated server.</p>

      <p>If you need zero-overhead series function call, check out</p>
      <p><a href="http://npm.im/fastseries">fastseries</a>. For zero-overhead parallel</p>
      <p>function call, check out <a href="http://npm.im/fastparallel">fastparallel</a>.</p>

      <ul>
      <li><a href="#install">Installation</a></li>
      <li><a href="#usage">Usage</a></li>
      <li><a href="#api">API</a></li>
      <li><a href="#license">Licence &amp; copyright</a></li>
      </ul>

      <h3>Install</h3>

      <p><code>npm i fastq --save</code></p>

      <h3>Usage (callback API)</h3>

      <pre><code>
      '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)
      }
      </code></pre>

      <h3>Usage (promise API)</h3>

      <pre><code>
      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()
      </code></pre>

      <h4>Setting "this"</h4>

      <pre><code>
      '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)
      }
      </code></pre>

      <h4>Using with TypeScript (callback API)</h4>

      <pre><code>
      '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)
      }
      </code></pre>

      <h4>Using with TypeScript (promise API)</h4>

      <pre><code>
      '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)
      }
      </code></pre>

      <h3>API</h3>

      <ul>
      <li><a href="#fastqueue"><code>fastqueue()</code></a></li>
      <li><a href="#push"><code>queue#<b>push()</b></code></a></li>
      <li><a href="#unshift"><code>queue#<b>unshift()</b></code></a></li>
      <li><a href="#pause"><code>queue#<b>pause()</b></code></a></li>
      <li><a href="#resume"><code>queue#<b>resume()</b></code></a></li>
      <li><a href="#idle"><code>queue#<b>idle()</b></code></a></li>
      <li><a href="#length"><code>queue#<b>length()</b></code></a></li>
      <li><a href="#getQueue"><code>queue#<b>getQueue()</b></code></a></li>
      <li><a href="#kill"><code>queue#<b>kill()</b></code></a></li>
      <li><a href="#killAndDrain"><code>queue#<b>killAndDrain()</b></code></a></li>
      <li><a href="#error"><code>queue#<b>error()</b></code></a></li>
      <li><a href="#concurrency"><code>queue#<b>concurrency</b></code></a></li>
      <li><a href="#drain"><code>queue#<b>drain</b></code></a></li>
      <li><a href="#empty"><code>queue#<b>empty</b></code></a></li>
      <li><a href="#saturated"><code>queue#<b>saturated</b></code></a></li>
      <li><a href="#promise"><code>fastqueue.promise()</code></a></li>
      </ul>

      <hr>
      <p><a name="fastqueue"></a></p>
      <h4>fastqueue([that], worker, concurrency)</h4>

      <p>Creates a new queue.</p>

      <p>Arguments:</p>

      <ul>
      <li><code>that</code>, optional context of the <code>worker</code> function.</li>
      <li><code>worker</code>, worker function, it would be called with <code>that</code> as <code>this</code>,</li>
      </ul>
      <p> if that is specified.</p>
      <ul>
      <li><code>concurrency</code>, number of concurre</li>
      </ul>

      <hr>
      <p>踩过坑的来评论区说说。</p>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors