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

358
Visualizações
How to implement two-way communication between nodejs and a python subprocess

I've got a node.js application that needs to pass input to a python program and get the input back. This process needs to occur frequently and with as little latency as possible. Thus I've chosen to use the child_process package. The javascript code is below

child_process = require('child_process');

class RFModel {

    constructor () {

        this.answer = null;
        this.python = child_process.spawn('python3', ['random_forest_model.py']);

        this.python.stdout.on('data', (data)=>{
            this.receive(data);
        });
        
    }

    send (data) {
        this.answer = null;
        this.python.stdin.write(data);
    }

    receive (data) {
        data = data.toString('utf8')
        console.log(data)
        this.answer = data;
    }

}

const model = new RFModel()

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
(async ()=>{
    await timeout(7000)
    model.send('asdf')
})();

And this is the python code:

import sys

sys.stdout.write('test')
sys.stdout.flush()

for line in sys.stdin:
    open("test.txt", "a") #janky test if input recieved

    sys.stdout.write(line)
    sys.stdout.flush()

The nodejs process receives the first "test" output from the python process, but it does not seem like python is receiving any input from nodejs, since there is no echo or text file being created. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm not a python expert, but presumably, for line in sys.stdin: is expecting an actual line (a series of characters followed by a line break character). But, from your nodejs program you aren't sending a line break character. You're just sending a series of characters. So your Python program is still waiting for the line break character to signify the end of the line.

Try changing this:

model.send('asdf')

to this:

model.send('asdf\n')

When I run it now with this change, I get this output:

test
asdf

Where there's a delay before the asdf shows up.

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