Tag: asynchronous

NodeJS:将大量同步任务分解为asynchronous任务

我正在处理大量的工作,然后写入数据库。 工作stream程是: 将大约100 MB的数据读入缓冲区 循环访问数据,然后处理(同步工作)并写入磁盘(asynchronous工作) 我遇到的问题是,它将完成所有100 MB数据的循环,同时将所有写入磁盘的事务循环排队。 所以,它将首先遍历所有的数据,然后运行asynchronous作业。 我想打破迭代通过数组的同步任务,以便每个迭代排队作为后面的事件循环。 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 […]

Node.js中的asynchronoushttp.getcallback

在Node中,我有这个函数片断(大大减less了实际的function,所以希望我没有削减任何重要的东西): Driver.prototype.updateDevices = function() { for (ip in this.ips) { var curIp = ip; if (this.ips[curIp]) { // in case this.ips[curIp] is set to undefined… http.get( { host: curIp, port: 80, path: '/tstat' }, function (res) { var result = ''; res.on('data', function (chunk) { result += chunk; }); res.on('end', function () { // Want […]

如何处理与MongoDB和node.js的asynchronous?

在这个代码片段中更新MongoDB数据库之前 , 确保for循环完成其处理的最好方法是: var userIdArray = [] // contains 100000 userIds User.find({'_id': {$in: userIdArray}}, 'name', function(err, result){ var thisTime = new Date().getTime(); var usersArray = []; for (var i = 0; i < result.length; i++) { var user = result[i]; var userObject = { userId: user['_id'], userName: user.name, time: thisTime } usersArray.push(userObject); }; Model.update(id, {$pullAll: […]

Nodejs串联运行函数

所以现在我试图使用Nodejs来访问文件,以便将它们写入服务器并处理它们。 我已经把它分成以下几个步骤: 遍历目录以生成所有文件path的数组 将来自每个文件path的原始文本数据放在另一个数组中 处理原始数据 前两个步骤工作正常,使用这些function: var walk = function(dir, done) { var results = []; fs.readdir(dir, function(err, list) { if (err) return done(err); var pending = list.length; if (!pending) return done(null, results); list.forEach(function(file) { file = path.resolve(dir, file); fs.stat(file, function(err, stat) { if (stat && stat.isDirectory()) { walk(file, function(err, res) { results = results.concat(res); […]

在NodeJS中使用stream和asynchronous读取和处理大小文件

我在逐行处理文件列表时遇到问题。 这是我正在使用的代码: var LineReader = require("line-by-line"); var async = require("async"); var files = [ "small.txt", "medium.txt", "large.txt" ]; var queue = async.queue(function(task, next){ console.log(task); next(); }, 10); async.eachSeries( files, function (file, callback) { var lineReader = new LineReader(file, { encoding: "utf8", skipEmptyLines: true }); lineReader.on("error", function (err) { callback(err); }); lineReader.on("line", function (line) { lineReader.pause(); […]

async.series和async.each不按预期方式工作

我正在尝试使用nodeJS构build一个网页抓取工具,该工具会search网站的HTML图像,caching图像源URL,然后search最大尺寸的url。 我遇到的问题是,在图像源URL数组循环以获取文件大小之前, deliverLargestImage()正在触发。 我正在尝试使用async.series和async.each这个工作正常。 如何强制deliverLargestImage()等待getFileSizes()内部的getFileSizes()完成? JS var async, request, cheerio, gm; async = require('async'); request = require('request'); cheerio = require('cheerio'); gm = require('gm').subClass({ imageMagick: true }); function imageScraper () { var imgSources, largestImage; imgSources = []; largestImage = { url: '', size: 0 }; async.series([ function getImageUrls (callback) { request('http://www.example.com/', function (error, response, html) { […]

在一个节点函数中进行数据库调用

我有一个函数,我打电话,我需要在该函数内进行一些数据库查询。 什么是在节点做这个最好的方法? 目前我只是做一个请求,并将返回值分配给一个variables,但这导致variables不被设置。 例: // bunch of code var is_member = false; mysql.query('SELECT EXISTS ( SELECT * FROM `qazusers` WHERE `qazemail` = ? OR `otheremail` = ? LIMIT 1)', [emailaddress, emailaddress], function (err, result) { if (err) { logger.info('Error checking. ', err); } logger.info('checkmembership: ', result); if (result[0] === 1) { is_member = true; } […]

如何与其他asynchronous模块一起使用asynchronous?

这是一个没有asynchronous的工作testing程序: var fs = require('fs'); function test() { var finalResponse = '', response = ''; function showFinalResponse() { console.log(finalResponse); } function processTheFile(err, data) { if (err) { finalResponse = 'Could not read the file'; } else { response += data; response += '</body></html>'; finalResponse = response; } showFinalResponse(); } function readTheFile(exists) { if (!exists) { […]

Nodejsasynchronous数据重复

我在nodejs上有一个asynchronous进程有问题。 我从远程JSON获取一些数据,并将其添加到我的数组中,这个JSON有一些重复的值,我需要检查它是否已经存在于我的数组之前,添加它,以避免数据重复。 我的问题是,当我启动JSON值之间的循环,循环调用下一个值,在最后一个过程完成之前,所以,我的数组充满重复的数据,而不是保持每个types只有一个项目。 看我目前的代码: BookRegistration.prototype.process_new_books_list = function(data, callback) { var i = 0, self = this; _.each(data, function(book) { i++; console.log('\n\n ———————————————————— \n\n'); console.log('BOOK: ' + book.volumeInfo.title); self.process_author(book, function() { console.log('in author'); }); console.log('\n\n ————————————————————'); if(i == data.length) callback(); }) } BookRegistration.prototype.process_author = function(book, callback) { if(book.volumeInfo.authors) { var author = { name: book.volumeInfo.authors[0].toLowerCase() }; […]

Superagent在asynchronous瀑布中移动响应callback位置

我有一个简单的工作superagent / async瀑布请求,看起来像这样: request = require 'superagent' user = request.agent() async.waterfall [ (cb)-> user.post('http://localhost:3000/form').send(name: 'Bob').end(cb) ], (err, res)-> console.log err console.log res 这成功地打印我的完整http响应,而err是undefined 。 如果我用一个额外的步骤完成同样的事情: request = require 'superagent' user = request.agent() async.waterfall [ (cb)-> user.post('http://localhost:3000/form').send(name: 'Bob').end(cb) (err, res)-> # this is never reached cb() ], (err, res)-> console.log err # this now prints out […]