g*********e 发帖数: 14401 | 1 比如我有类似
for (i =0; i
connection(select... where testname=testnames[i]) function(rows, )...
...
}
最后我想把这些rows都Push到一个array里,然后用json得形式返回response。
但node里query 似乎是non blocking的?这种一般怎么解决? |
l**********n 发帖数: 8443 | |
p*****2 发帖数: 21240 | 3
很简单呀。造一个array,每次query返回的时候检查array是否够数了,够了就返回了
。
【在 g*********e 的大作中提到】 : 比如我有类似 : for (i =0; i: connection(select... where testname=testnames[i]) function(rows, )... : ... : } : 最后我想把这些rows都Push到一个array里,然后用json得形式返回response。 : 但node里query 似乎是non blocking的?这种一般怎么解决?
|
g*********e 发帖数: 14401 | 4 这个是标准做法吗?
【在 p*****2 的大作中提到】 : : 很简单呀。造一个array,每次query返回的时候检查array是否够数了,够了就返回了 : 。
|
d****n 发帖数: 1637 | 5 async.series
async.parallel
Async.waterfall
Async.map
总有一款适合你 |
p*****2 发帖数: 21240 | 6 是 因为其他人说的都需要额外的library去支持
【在 g*********e 的大作中提到】 : 这个是标准做法吗?
|
l**********n 发帖数: 8443 | 7 Promise是自带的, es6
【在 p*****2 的大作中提到】 : 是 因为其他人说的都需要额外的library去支持
|
p*****2 发帖数: 21240 | 8 现在用不起来吧?
【在 l**********n 的大作中提到】 : Promise是自带的, es6
|
l**********n 发帖数: 8443 | 9 咋用不起来,node支持啊,又不是generator和es7,需要iojs,连browser都大量支持
【在 p*****2 的大作中提到】 : 现在用不起来吧?
|
p*****2 发帖数: 21240 | 10 什么时候支持的?
http://stackoverflow.com/questions/21564993/native-support-for-
【在 l**********n 的大作中提到】 : 咋用不起来,node支持啊,又不是generator和es7,需要iojs,连browser都大量支持
|
|
|
l**********n 发帖数: 8443 | |
l**********n 发帖数: 8443 | 12 I use bluebird. but I also use node.12. so I am not wrong
【在 p*****2 的大作中提到】 : 什么时候支持的? : http://stackoverflow.com/questions/21564993/native-support-for-
|
p*****2 发帖数: 21240 | 13 上边说了node自带的有问题吧?
【在 l**********n 的大作中提到】 : I use bluebird. but I also use node.12. so I am not wrong
|
d****n 发帖数: 1637 | 14 var async = require('async')
async.map(testNames,
function processOneName(tname, cb){
connectionToEachDb(tname,
function (rows, err){
if (err){
return cb(err);
}
return cb( null, rows);
}
);
},
function allDone(err, results){
if (err) return console.log("A error:", err);
return results ; // results are in the order of your testNames if all
successed
}
);
//use async.mapLimit([], limits, function procEach(){}, function done(){})
//if you want a bounded number of connections at same time
【在 g*********e 的大作中提到】 : 比如我有类似 : for (i =0; i: connection(select... where testname=testnames[i]) function(rows, )... : ... : } : 最后我想把这些rows都Push到一个array里,然后用json得形式返回response。 : 但node里query 似乎是non blocking的?这种一般怎么解决?
|
d****n 发帖数: 1637 | 15 你着个有多个callback的风险吧。unsafe
但是小qps就无所谓了。
【在 p*****2 的大作中提到】 : 是 因为其他人说的都需要额外的library去支持
|
p*****2 发帖数: 21240 | 16
多个callback是什么风险呀?能ZKSS为什么unsafe吗?
【在 d****n 的大作中提到】 : 你着个有多个callback的风险吧。unsafe : 但是小qps就无所谓了。
|
l**********n 发帖数: 8443 | 17 你node version是啥?我现在用nvm, 和iojs
【在 p*****2 的大作中提到】 : 上边说了node自带的有问题吧?
|
p*****2 发帖数: 21240 | 18
iojs不是node fork出去的吗? 我的版本是v0.10.26
【在 l**********n 的大作中提到】 : 你node version是啥?我现在用nvm, 和iojs
|
l**********n 发帖数: 8443 | 19 难怪。
【在 p*****2 的大作中提到】 : : iojs不是node fork出去的吗? 我的版本是v0.10.26
|
d****n 发帖数: 1637 | 20 你的array 不用 lock么?
不同时间的query 返回,都要 arr.push(result), 这不是明显的racing问题么?
还有
var results = [];
var numWantedResults = 10 ;
for ( i=0;i
ret = connectToDb(.., function (err, rows){
results.push(rows);
//你能保证length, push都同步么?最后一个回来的query 都认为自己是倒数最
后一个,
//永远也不能返回了。
//double callback也有可能,在每个query都认为自己是最后一个的情况,
//你的系统就 咣,咣,咣 了
if( results.length == numWantedResults ){
return results; //
}
})
}
如果加lock,sycn那和async, promise 有什么区别?
【在 p*****2 的大作中提到】 : : iojs不是node fork出去的吗? 我的版本是v0.10.26
|
|
|
p*****2 发帖数: 21240 | 21
你真的学过Node吗?
【在 d****n 的大作中提到】 : 你的array 不用 lock么? : 不同时间的query 返回,都要 arr.push(result), 这不是明显的racing问题么? : 还有 : var results = []; : var numWantedResults = 10 ; : for ( i=0;i: ret = connectToDb(.., function (err, rows){ : results.push(rows); : //你能保证length, push都同步么?最后一个回来的query 都认为自己是倒数最 : 后一个,
|
d****n 发帖数: 1637 | 22 一点点
【在 p*****2 的大作中提到】 : : 你真的学过Node吗?
|
d****n 发帖数: 1637 | 23 如果你写成这样,为啥不用lib?多难看
process.nextTick(function() {
callback(er);
});
你写一个看看
【在 p*****2 的大作中提到】 : : 你真的学过Node吗?
|
p*****2 发帖数: 21240 | 24
怪不得呢。
【在 d****n 的大作中提到】 : 一点点
|
d****n 发帖数: 1637 | 25 敢写个看看不?
【在 p*****2 的大作中提到】 : : 怪不得呢。
|
p*****2 发帖数: 21240 | 26
你也太有意思了。请问你如何加lock呢?
【在 d****n 的大作中提到】 : 敢写个看看不?
|
d****n 发帖数: 1637 | 27 lock 不lock就是个通用的比喻。Node lock 一个tick是时间感念,别的语言是空间概
念。
别说你不懂啥叫double callback。
你这个避免不了callback hell
【在 p*****2 的大作中提到】 : : 你也太有意思了。请问你如何加lock呢?
|
p*****2 发帖数: 21240 | 28 就不能先好好学学node在过来喷?
【在 d****n 的大作中提到】 : lock 不lock就是个通用的比喻。Node lock 一个tick是时间感念,别的语言是空间概 : 念。 : 别说你不懂啥叫double callback。 : 你这个避免不了callback hell
|
n*****t 发帖数: 22014 | 29 select where testname in (...)
或者 async.foreach |
d****n 发帖数: 1637 | 30 你这人嘴太贱,是北京特色么?
你自己觉得自己了不起,太井底蛙了。
你这么bitch,你手下人真是不幸啊。
以后觉不和你talk,你太asshole了。本版一毒瘤。
【在 p*****2 的大作中提到】 : 就不能先好好学学node在过来喷?
|
|
|
p*****2 发帖数: 21240 | 31 呵呵 你的嘴很好
我从不觉得自己了不起呀 但是事实就是事实呀
【在 d****n 的大作中提到】 : 你这人嘴太贱,是北京特色么? : 你自己觉得自己了不起,太井底蛙了。 : 你这么bitch,你手下人真是不幸啊。 : 以后觉不和你talk,你太asshole了。本版一毒瘤。
|
d****n 发帖数: 1637 | 32 事实就是你是个毒瘤。我好久没骂人了~
你白痴的时候还少么?不懂装懂,总是在这里套话。有意思么?
你帮助别人时候多还是讽刺别人时候多?
你从小没学过怎么和别人交流么?工作也这样么?
我承认是你天顶星最牛逼的人,可惜你不会说话。
【在 p*****2 的大作中提到】 : 呵呵 你的嘴很好 : 我从不觉得自己了不起呀 但是事实就是事实呀
|
n*****t 发帖数: 22014 | 33 楼上二位 。。。技术讨论吧 。。。
async 是显然的,lock 我不认为是问题,单线程的。楼主的例子,testname 可能在两
次 query 中被 update,导致同一条记录被 push 几次,再考虑效率,select in 我觉
得是最好的办法。
初学者,表喷我。。。。。。 |
p*****2 发帖数: 21240 | 34
testname 可能在两
次 query 中被 update,导致同一条记录被 push 几次,
这个是什么意思?
【在 n*****t 的大作中提到】 : 楼上二位 。。。技术讨论吧 。。。 : async 是显然的,lock 我不认为是问题,单线程的。楼主的例子,testname 可能在两 : 次 query 中被 update,导致同一条记录被 push 几次,再考虑效率,select in 我觉 : 得是最好的办法。 : 初学者,表喷我。。。。。。
|
p*****2 发帖数: 21240 | 35
LZ问的是一个general的情况吧?select in 不一定work吧?我觉得LZ就是举个例子。
现实中需要几条SQL同时运行然后aggregate结果。
【在 n*****t 的大作中提到】 : 楼上二位 。。。技术讨论吧 。。。 : async 是显然的,lock 我不认为是问题,单线程的。楼主的例子,testname 可能在两 : 次 query 中被 update,导致同一条记录被 push 几次,再考虑效率,select in 我觉 : 得是最好的办法。 : 初学者,表喷我。。。。。。
|
g*********e 发帖数: 14401 | 36 i have an array, each of them is a string of a testname.
then a need to do a sql query "select ... where TESTNAME=testname ... " for
each tests, then collect each query result back and form the eventual
response.
[{test1: test1_query_result},
{test2: test2_query_result},
...
]
that's what i want. can anyone share usable code? Thx. |
p*****2 发帖数: 21240 | 37 上边有人写了一个 拿来用就行了
for
【在 g*********e 的大作中提到】 : i have an array, each of them is a string of a testname. : then a need to do a sql query "select ... where TESTNAME=testname ... " for : each tests, then collect each query result back and form the eventual : response. : [{test1: test1_query_result}, : {test2: test2_query_result}, : ... : ] : that's what i want. can anyone share usable code? Thx.
|
g*********e 发帖数: 14401 | 38
which one? I found them all not usable...
【在 p*****2 的大作中提到】 : 上边有人写了一个 拿来用就行了 : : for
|
g*********e 发帖数: 14401 | 39
never mind, i solved this with async.map
lol
【在 p*****2 的大作中提到】 : 上边有人写了一个 拿来用就行了 : : for
|
l**********n 发帖数: 8443 | 40 first turn the node callback into promise, then use Promise.all. Is that
clear?
【在 g*********e 的大作中提到】 : : never mind, i solved this with async.map : lol
|
|
|
n*****t 发帖数: 22014 | 41 某记录,第一次 query name = tom,中间被其他程序 update 了,第二次 name =
marry
两次 query 用不同 name 得到同一个 userid
【在 p*****2 的大作中提到】 : 上边有人写了一个 拿来用就行了 : : for
|
p*****2 发帖数: 21240 | 42
这个跟node没关系吧?
【在 n*****t 的大作中提到】 : 某记录,第一次 query name = tom,中间被其他程序 update 了,第二次 name = : marry : 两次 query 用不同 name 得到同一个 userid
|
n*****t 发帖数: 22014 | 43 所以这个 sql 要重写
【在 p*****2 的大作中提到】 : : 这个跟node没关系吧?
|
p*****2 发帖数: 21240 | 44 为什么你assume别人会改db呢?
也许immutable的 至少testname我不感觉会被改来改去
【在 n*****t 的大作中提到】 : 所以这个 sql 要重写
|
g*********e 发帖数: 14401 | 45 no, my db is updated with a cronjob at midnight |
n*****t 发帖数: 22014 | 46 写程序的时候当然要考虑啊,即使现在没人update,你也不能保证将来没有,这不是马
工的基本素养吗?
就这个例子,最好的解决方案是 select in,一个 query 解决。其次是多条 select
into temp,一个 db connection。最后才是在 app 里面自己搞。
【在 p*****2 的大作中提到】 : 为什么你assume别人会改db呢? : 也许immutable的 至少testname我不感觉会被改来改去
|
p*****2 发帖数: 21240 | 47
那testnames也可能会变呀。
【在 n*****t 的大作中提到】 : 写程序的时候当然要考虑啊,即使现在没人update,你也不能保证将来没有,这不是马 : 工的基本素养吗? : 就这个例子,最好的解决方案是 select in,一个 query 解决。其次是多条 select : into temp,一个 db connection。最后才是在 app 里面自己搞。
|
n*****t 发帖数: 22014 | 48 那干脆别写了,你这不是抬杠嘛!
【在 p*****2 的大作中提到】 : : 那testnames也可能会变呀。
|
g*********e 发帖数: 14401 | 49
my table schema is like this:
[testname, function, time]
is it possible for sql to do nested results in single select?
desired result:
{testname1 {function11, time11;
function12, time12;
...}
testname2 {function21, time21;
function22, time22;
...}
testname3 {...}
}
【在 n*****t 的大作中提到】 : 写程序的时候当然要考虑啊,即使现在没人update,你也不能保证将来没有,这不是马 : 工的基本素养吗? : 就这个例子,最好的解决方案是 select in,一个 query 解决。其次是多条 select : into temp,一个 db connection。最后才是在 app 里面自己搞。
|
p*****2 发帖数: 21240 | 50
这个你groupby一下就行了吧?
【在 g*********e 的大作中提到】 : : my table schema is like this: : [testname, function, time] : is it possible for sql to do nested results in single select? : desired result: : {testname1 {function11, time11; : function12, time12; : ...} : testname2 {function21, time21; : function22, time22;
|
|
|
p*****2 发帖数: 21240 | 51
我觉得写test code不需要那么严谨,尤其是自己可以控制的情况下。
当然我不否定你的意思,在这个case下,确实用一个SQL就算了。
我想讨论的更多的是几个操作如何并行的问题。用native node怎么做。
【在 n*****t 的大作中提到】 : 那干脆别写了,你这不是抬杠嘛!
|
n****j 发帖数: 1708 | 52 SQL 没法给你这样的结果,只能是 table,select * from table where testname in
(n1,n2, ...) order by testname,然后在 node 里 foreach row , result[testname
].push({func,time})
【在 g*********e 的大作中提到】 : : my table schema is like this: : [testname, function, time] : is it possible for sql to do nested results in single select? : desired result: : {testname1 {function11, time11; : function12, time12; : ...} : testname2 {function21, time21; : function22, time22;
|
n****j 发帖数: 1708 | 53 groupby 只能做 sum average 这些
【在 p*****2 的大作中提到】 : : 我觉得写test code不需要那么严谨,尤其是自己可以控制的情况下。 : 当然我不否定你的意思,在这个case下,确实用一个SQL就算了。 : 我想讨论的更多的是几个操作如何并行的问题。用native node怎么做。
|
g*********e 发帖数: 14401 | 54 恩
in
testname
【在 n****j 的大作中提到】 : SQL 没法给你这样的结果,只能是 table,select * from table where testname in : (n1,n2, ...) order by testname,然后在 node 里 foreach row , result[testname : ].push({func,time})
|
p*****2 发帖数: 21240 | 55
啥意思?
【在 n****j 的大作中提到】 : groupby 只能做 sum average 这些
|
g*********e 发帖数: 14401 | 56
比如
[testname:abc, time:123]
[testname:abc, time:222]
[testname:def, time:333]
group by testname以后 中间那个就没有了
【在 p*****2 的大作中提到】 : : 啥意思?
|
p*****2 发帖数: 21240 | 57 为什么你会这么理解?
【在 g*********e 的大作中提到】 : : 比如 : [testname:abc, time:123] : [testname:abc, time:222] : [testname:def, time:333] : group by testname以后 中间那个就没有了
|
p*****2 发帖数: 21240 | 58 我的意思是你用node的groupby
【在 g*********e 的大作中提到】 : : 比如 : [testname:abc, time:123] : [testname:abc, time:222] : [testname:def, time:333] : group by testname以后 中间那个就没有了
|
n****j 发帖数: 1708 | 59 underscore? 以为你说 sql 一把抓出来呢
【在 p*****2 的大作中提到】 : 我的意思是你用node的groupby
|
p*****2 发帖数: 21240 | 60
我用func-coffee
昨天看了一下context,确实没说清楚。我说的是fp里的groupby,不是sql里的。
【在 n****j 的大作中提到】 : underscore? 以为你说 sql 一把抓出来呢
|
|
|
h******b 发帖数: 6055 | 61 这年头用JavaScript的没人不用underscore吧,太好用了。
【在 n****j 的大作中提到】 : underscore? 以为你说 sql 一把抓出来呢
|
N*****m 发帖数: 42603 | 62 不用的多了去了
用lodash。:D
【在 h******b 的大作中提到】 : 这年头用JavaScript的没人不用underscore吧,太好用了。
|
p*****2 发帖数: 21240 | 63 我不用 我写了一个跟scala兼容的
【在 h******b 的大作中提到】 : 这年头用JavaScript的没人不用underscore吧,太好用了。
|
h******b 发帖数: 6055 | 64 这个好像是underscore的马甲? 看上去连syntax都一样。 你觉得比underscore好吗
?
【在 N*****m 的大作中提到】 : 不用的多了去了 : 用lodash。:D
|
h******b 发帖数: 6055 | 65 underscore这种library大大降低了编程的门槛。 我这种不会算法的也能搞数据处理
了。 超级好用啊。
【在 p*****2 的大作中提到】 : 我不用 我写了一个跟scala兼容的
|
p*****2 发帖数: 21240 | 66
我喜欢OO风格的。
【在 h******b 的大作中提到】 : underscore这种library大大降低了编程的门槛。 我这种不会算法的也能搞数据处理 : 了。 超级好用啊。
|