0

I have a Node.js script collecting realtime data from motion sensors. I would like to pass each frame of data to a Python script and process the data there. I found a guide on how to send data to Python, but it just opens a lot of Python processes.

Is there a better way to send pass data from Node.js to Python to proces realtime data?

server.js

transFunc:function( component, parameters )
    {

        data2Python = Object.values(parameters).join()';

        const spawn = require('child_process').spawn;

        const process = spawn('python', ['./calculate_risk.py', data2Python]);
        process.stdout.on('data', function (data) {
            res.send(data.toString());
        });

    }

calculations.py

import pandas as pd
import sys

data = int(sys.argv[1]) # 3

def calculate_stuff(data):

    # do something with the data
    print(data)
    sys.stdout.flush()

calculate_stuff(data)
2
  • what on earth? this is no C#, change your brackets, its killing me. Also you need to watch out, this might cause you with a bugs. JS puts ";" after each line. Commented Oct 15, 2020 at 13:15
  • You could write the node.js data to a file and read it it with python. Keep the python process alive by checking if the file has changed. Commented Oct 15, 2020 at 13:16

1 Answer 1

1

Is there a better way to send pass data from Node.js to Python to proces realtime data?

You can use a python framework like Flask and expose an API that will do the pandas part. It will return the result to the node client.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.