Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

292
Vistas
Can console.log()s called from two different Worker threads ever step on each other, or are they always one after the other?

I have this small piece of example code:

//main.js

const {Worker} = require("worker_threads");

const worker = new Worker("./background.js", {workerData: {}});

function loop() {

    console.log("main: 1234567890");
    setTimeout(loop, 0)

}

loop();

And:

//background.js

const {parentPort, workerData} = require("worker_threads");

function loop () {

    console.log("background: 1234567890");
    setTimeout(loop, 0)

}

loop();

Run like so: node main.js

The output is this:

background: 1234567890
main: 1234567890
background: 1234567890
main: 1234567890
background: 1234567890
background: 1234567890
#etc

Sometimes several console.log()'s from one thread are called before any are called by the other. This is expected, and fine.

But is there any chance that a console.log() could be called in the middle of another console.log()? So for example, could this happen?

background: 123main: 12345456786789090

I haven't observed it in my output, but I'd like to know from a canonical source that Workers/console.log() do not work that way in node.js.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

console.log() for primitives (non-objects) is thread-safe. So, in your particular example where you're passing a string, that is safe and you won't get mixed up output.

But, if you pass an object to console.log(obj) and threads are in the process of modifying that object, then there can be issues before the object is turned into output and marshalled over to the logging output. This can even happen with just a single thread where you immediately modify the object on subsequent lines of code.

This apparently occurs because the conversion of the object to the console output is "lazy" and not immediate. This is probably an attempt to run this in parallel in order to minimize the performance impact of calls to console.log(). I've seen this in real code and the work-around was to call console.log(JSON.stringify(obj)) rather than console.log(obj) to remove any chance of an issue with the object being modified before it got lazily logged.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda