簡體   English   中英

在 Python 中處理來自 Node.js 的實時數據

[英]Proces realtime data from Node.js in Python

我有一個 Node.js 腳本從運動傳感器收集實時數據。 我想將每一幀數據傳遞給 Python 腳本並在那里處理數據。 我找到了一個關於如何將數據發送到 Python 的指南,但它只是打開了很多 Python 進程。

有沒有更好的方法將傳遞數據從 Node.js 發送到 Python 以處理實時數據?

服務器.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());
        });

    }

計算.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)

有沒有更好的方法將傳遞數據從 Node.js 發送到 Python 以處理實時數據?

您可以使用像Flask這樣的 Python 框架並公開一個 API 來完成 Pandas 部分。 它將結果返回給節點客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM