Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

291
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda