Live data from Hacker News

On problems with threads in Node.js

future-processing.pl

11–20 of 66 posts

Re: On problems with threads in Node.js

#11
post #10

Is this another case of "here's the code I ran" when in fact they didn't? There should be 3 lines of output, not 6! Also, the code says it will print the time taken since the start of the program, which again doesn't go with the output and the conclusion being made! Anyway, how come the output isn't in order?

Key line before where he shows output...

> However, watch what happens if we double the number of iterations

Re: On problems with threads in Node.js

#12
post #5

To sum it up: node.js runs only 4 background threads. Be aware of this. No big deal really. I have multiple servers running node.js under load for 3+ years and never had an issue with this. In fact, I found it helpful. If your database is under load and is already running 4 heavy queries, not giving it any more jobs is actually a good thing.

> In fact, I found it helpful. If your database is under load and is already running 4 heavy queries, not giving it any more jobs is actually a good thing.

Actually the threadpool shouldn't be a factor then, unless there are no nonblocking/async drivers for the database: internally, libuv uses its threadpool to asyncify operations for which no async/nonblocking version exists (Erlang does the same IIRC), for the most part it's filesystem operations, getaddrinfo and getnameinfo (I'm guessing it could include other stuff depending on what the underlying OS provides).

Socket or file IO should have native non-blocking APIs, so it does not need to use the threadpool.

Re: On problems with threads in Node.js

#13

It's worth noting that DB drivers that actually integrate with libuv are a minority - most DB divers use Node-level network APIs and are unaffected by such thread pool limits.

Even when the db drivers integrate with libuv, there is little reason to use blocking APIs for that. The threadpool is primarily used for operations for which no non-blocking API is available (mostly filesystem access).

Re: On problems with threads in Node.js

#14
post #11
post #10

Is this another case of "here's the code I ran" when in fact they didn't? There should be 3 lines of output, not 6! Also, the code says it will print the time taken since the start of the program, which again doesn't go with the output and the conclusion being made! Anyway, how come the output isn't in order?

Key line before where he shows output... > However, watch what happens if we double the number of iterations

Ah yes, completely skipped over that bit :P

Re: On problems with threads in Node.js

#15
post #10

Is this another case of "here's the code I ran" when in fact they didn't? There should be 3 lines of output, not 6! Also, the code says it will print the time taken since the start of the program, which again doesn't go with the output and the conclusion being made! Anyway, how come the output isn't in order?

Oops, thanks for that - seems the results from the first run of the example somehow got lost in the final version and I didn't notice.

The order of the output is dependent on when each call finished - they run in parallel, so it's not guaranteed that functions will end in the order they were invoked.

Re: On problems with threads in Node.js

#16
post #2

Piece of badly engineered software conquering the world. As if more JavaScript was really something worth pursuing.

Do you realize that comments like this are what is breaking the community here at HN?

Are you aware that people like you are destroying something that was once brilliant?

HN is going through an Eternal September.

Re: On problems with threads in Node.js

#18
post #2

Piece of badly engineered software conquering the world. As if more JavaScript was really something worth pursuing.

Well, it's not perfect, but you could do a lot worse than use JavaScript. Lots of R&D at companies and organisations like Google, Microsoft, Mozilla and Intel is going into JavaScript (likely billions), and as far as I'm concerned that development is resulting in a pretty useful (and increasingly performant) language for doing what people use it for: web platform applications.

Re: On problems with threads in Node.js

#19
post #15
post #10

Is this another case of "here's the code I ran" when in fact they didn't? There should be 3 lines of output, not 6! Also, the code says it will print the time taken since the start of the program, which again doesn't go with the output and the conclusion being made! Anyway, how come the output isn't in order?

Oops, thanks for that - seems the results from the first run of the example somehow got lost in the final version and I didn't notice. The order of the output is dependent on when each call finished - they run in parallel, so it's not guaranteed that functions will end in the order they were invoked.

Ah yes, so they do. My lack of sleep is showing!

For some reason I was thinking the readdir would run in series so output would go up by ~1s each time.

Post reply on HN