未加星标

Nodejs MySQL Query Blocks Request Module

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I'm a nodejs newbie and getting used to all the async stuff.

Using the: - Node-mysql (https://github.com/felixge/node-mysql) - Request (https://github.com/mikeal/request) modules right now.

I'm querying a big list of URLs (10,000 URLs) and trying to send them a HTTP GET request to get their HTTP statuses.

It works but the problem is, when I run the code, when each row from the MySQL query is returned, the processURL function is called, it logs the 'BEFORE' and 'AFTER' just after the each URL value, (as far as I can understand) sends the HTTP request, but waits for the MySQL query to complete before returning the HTTP statuses.

Simply, it can not check these URLs and return their results while the query is running. It waits for the query to complete to return the results of the HTTP requests.

This looks meaningless to me as it runs the processURL function when each row is streamed. Here is the full code:

var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'urlDB', }); var query = connection.query('SELECT * FROM urls LIMIT 10000'); query.on('error', function(err) { console.log(err); }) query.on('result', function(row) { processURL(row.urlValue, function() { }); console.log(row.urlValue); }) function processURL (url){ var request = require('request'); var startTime = new Date(); console.log('BEFORE'); request({ uri: url, method: 'GET', }, function(error, response, body) { console.log('INSIDE'); var endTime = new Date(); if (!error) { console.log(response.statusCode + ' Start: ' + startTime + ' End: ' + endTime); } else { console.log('timeout'); } }) console.log('AFTER'); }

The output is as below: www.google.com BEFORE AFTER www.yahoo.com BEFORE AFTER www.cnn.com BEFORE AFTER ...but no 'INSIDE' until the query completes.

Any help is appreciated and thanks very much.

Problem courtesy of: umutm

Solution

I modified your example to get results more slowly, and it works as expected. You can copy-paste to try:

var request = require('request'); var urls = ['http://google.com', 'http://yahoo.com', 'http://ninjaturtles.com']; getResultSlowly(); function getResultSlowly() { var result = urls.shift(); if (!result) return; console.log(result); processUrl(result); setTimeout(getResultSlowly, 1000); } function processUrl(url) { var startTime = new Date(); console.log('BEFORE'); request({ uri: url, method: 'GET', }, function(error, response, body) { console.log('INSIDE'); var endTime = new Date(); if (!error) { console.log(response.statusCode + ' Start: ' + startTime + ' End: ' + endTime); } else { console.log('timeout'); } }) console.log('AFTER'); }

It's likely that network latency is just covering up the async behavior.

The results from your query will return near-instantly, firing several result events as soon as you execute your code. However, your request event will take quite a bit longer (even if just a millisecond or two) because it has to go make a connection with google or yahoo or whomever.

output:

$ node asynctest.js http://google.com BEFORE AFTER INSIDE 200 Start: Sat Dec 29 2012 11:31:34 GMT-0500 (EST) End: Sat Dec 29 2012 11:31:34 GMT-0500 (EST) http://yahoo.com BEFORE AFTER http://ninjaturtles.com BEFORE AFTER INSIDE 200 Start: Sat Dec 29 2012 11:31:36 GMT-0500 (EST) End: Sat Dec 29 2012 11:31:37 GMT-0500 (EST) INSIDE 200 Start: Sat Dec 29 2012 11:31:35 GMT-0500 (EST) End: Sat Dec 29 2012 11:31:38 GMT-0500 (EST)

Solution courtesy of: hunterloftis

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

tags: console,log,var,function,query,request,EST,Sat,GMT
分页:12
转载请注明
本文标题:Nodejs MySQL Query Blocks Request Module
本站链接:https://www.codesec.net/view/604999.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(8)