3

The print statement in Python is not thread-safe. Is it safe to use console.log in Node.js concurrently?

If so, then is it also interleave-safe? That is, if multiple (even hundreds) of callbacks write to the console, can I be sure that the output won't be clobbered or interleaved?

Looking at the source code, it seems that Node.js queues concurrent attempts to write to a stream (here). On the other hand, console.log's substitution flags come from printf(3). If console.log wraps around printf, then that can interleave output on POSIX machines (as shown here).

Please show me where the async ._write(chunk, encoding, cb) is implemented inside Node.js in your response to this question.

EDIT: If it is fine to write to a stream concurrently, then why does this npm package exist?

Community
  • 1
  • 1
Coder
  • 597
  • 7
  • 22
  • Node.js is single threaded. Callbacks may originate from different threads, but once they enter the main thread, nothing is concurrent. – 4castle Aug 10 '16 at 03:20

3 Answers3

4

Everything in node.js is basically "atomic". That's because node.js is single threaded - no code can ever be interrupted.

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • Note though; there are issues with `console.log` object evaluation being lazy in browsers. See http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays. Shouldn't impact node.js apps, though. – Jacob Aug 10 '16 at 03:19
  • Can someone please explain how can Node be single-threaded and still from callback hell? I mean.. What you're saying is that, even though there may be 2000 callbacks going on, just one of them will be executed at a time until it's finished or blocked by a non-CPU bound operation? – Andre Pena Aug 10 '16 at 03:26
  • 3
    @andrerpena: Yes, only one of them will be executed at any time, but all 2000 can be WAITING in parallel. That's what concurrency means - code can WAIT in parallel without executing. Concurrency != parallelism. – slebetman Aug 10 '16 at 03:28
  • OK, but 'atomic' writes by character? By a newline? Also please see the edits I made since you answered this question. I understand in theory what you mean, but I need a deeper explanation. – Coder Aug 10 '16 at 03:32
  • @andrerpena: Here are two previous answers I've written explaining the architecture: http://stackoverflow.com/questions/19616477/does-javascript-process-using-an-elastic-racetrack-algorithm/19620041#19620041, http://stackoverflow.com/questions/29883525/i-know-that-callback-function-runs-asynchronously-but-why/29885509#29885509 – slebetman Aug 10 '16 at 03:34
  • @Coder: No, atomic means the execution of the `console.log()` function itself, including the internal call to `printf()` etc. No function in js actually write directly to stdout. Instead it queues the writing in the event loop (the code you found). So `atomic` writes by "message". – slebetman Aug 10 '16 at 03:36
  • @Coder: Note that node.js uses one thread for everything: console, sockets etc. The only exception is disk I/O, then node uses one thread per file which still means that I/O is technically thread safe. – slebetman Aug 10 '16 at 03:39
  • @slebetman: I can accept your answer if you amend it to include where the NPM package comes from and where the async _write function is implemented in the source code. – Coder Aug 10 '16 at 18:02
  • The NPM package is to handle the case where TWO programs write to the same file. His solution is to write to a temp file and THEN copy it to place (on unix rename is atomic: http://stackoverflow.com/questions/18706419/is-a-move-operation-in-unix-atomic). This is to handle the case where the node process has race condition with another node process or a C program or a Ruby process or a Perl process etc. – slebetman Aug 10 '16 at 23:00
1

The events loop of nodejs is single thread, but all the async calls of nodejs are multi-threaded, it use libuv under the hood, libuv is library that use multi threads.

link: https://medium.com/the-node-js-collection/what-you-should-know-to-really-understand-the-node-js-event-loop-and-its-metrics-c4907b19da4c

Adam va
  • 11
  • 2
0

Based on what I see on my Node.js console it is NOT "interleave-safe".

I can see my console-output is sometimes "clobbered or interleaved". Not always. When I run my program it is maybe every 5th time that I see interleaved output from multiple log-statements.

This may of course depend on your Node.js version and the OS you are running it on. For the record my Node.js version is v12.13.0 and OS is Windows 10.0.19042.

Panu Logic
  • 2,193
  • 1
  • 17
  • 21