node.js - NodeJS : Breaking a large amount of synchronous tasks into async tasks

标签 node.js asynchronous

我正在处理大量作业,然后将它们写入数据库。工作流程是这样的:

  1. 将大约 100 MB 数据读取到缓冲区
  2. 循环数据,然后处理(同步工作)并写入磁盘(异步工作)

我遇到的问题是,它将完成所有 100 MB 数据的循环,同时将事件循环后面的所有磁盘写入排队。因此,它将首先迭代所有数据,然后运行异步作业。

我想中断遍历数组的同步任务,以便每次迭代都在事件循环后面排队。

var lotsOfWorkToBeDone = ['tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job']

while (true) {
  var job = lotsOfWorkToBeDone.pop()
  if (!job) {
    break
  }
  var syncResult = syncWork(job)
  asyncWork(syncResult)
}

function syncWork(job) {
  console.log('sync work:', job)
  return 'Sync Result of ' + job
};

function asyncWork(syncResult) {
  setTimeout(function() {
    console.log('async work: ', syncResult)
  }, 0)
}


// Desire Outcome
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job

// Actual Outcome
// sync work: tens of thousands of job
// sync work: tens of thousands of job
// sync work: tens of thousands of job
// sync work: tens of thousands of job
// sync work: tens of thousands of job
// sync work: tens of thousands of job
// sync work: tens of thousands of job
// async work: Sync Result of tens of thousands of job
// async work: Sync Result of tens of thousands of job
// async work: Sync Result of tens of thousands of job
// async work: Sync Result of tens of thousands of job
// async work: Sync Result of tens of thousands of job
// async work: Sync Result of tens of thousands of job
// async work: Sync Result of tens of thousands of job

注意:该示例是现实的简化版本。我没有可以迭代的数组。我有一个很大的缓冲区,我会处理直到 EOF(因此,while 循环)

最佳答案

使用 async.whilst 似乎达到了预期的结果。

我暂时不会接受自己的答案,因为我对人们对此解决方案的评论感兴趣。可能有更好的解决方案

var async = require('async')
var lotsOfWorkToBeDone = ['tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job', 'tens of thousands of job']

var job;
async.whilst(function() {
  job = lotsOfWorkToBeDone.pop()
  return job
}, function(callback) {
  var syncResult = syncWork(job)
  asyncWork(syncResult, callback)
}, function(err) {
  console.log('error: ', err)
})

function syncWork(job) {
  console.log('sync work:', job)
  return 'Sync Result of ' + job
};

function asyncWork(syncResult, callback) {
  setTimeout(function() {
    console.log('async work: ', syncResult)
    callback()
  }, 0)
}

// sync work: tens of thousands of job
// async work:  Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work:  Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work:  Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work:  Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work:  Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work:  Sync Result of tens of thousands of job
// sync work: tens of thousands of job
// async work:  Sync Result of tens of thousands of job

关于node.js - NodeJS : Breaking a large amount of synchronous tasks into async tasks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795480/

相关文章:

javascript - 在 if 语句中对数组使用 indexOf 的结果不一致

javascript - 在 Mongoose 的字段中定义一个函数作为默认值

javascript - 为什么 Node.js 没有原生 DOM?

javascript - 了解为什么Adword再营销标记的<script>标记中不能使用 "async"

c# - 异步等待 Task.Run 与 HttpClient.GetAsync

jquery - 表单验证和 HTML 修改

node.js - 部分人可以在 sails 中访问本地人吗?为什么不?

node.js - 第一次失败后如何停止 async.queue?

mysql - 在 Spring 应用程序的事务中使用 Async

javascript - 使用 Q/promises/异步函数时,如何将值从一个类方法返回到另一个类方法?