Unexpectedly, NodeJS can also call Python?

Author : xuzhiping   2022-11-24 14:11:50 Browse: 1056
Category : Python

Abstract: I found it interesting to read an article today. Unexpectedly, NodeJS can also call Python. As we all know, Python language has...

I found it interesting to read an article today. Unexpectedly, NodeJS can also call Python. As we all know, Python language has become extremely popular in recent years. Combining with the popular JavaScript, it gives us more imagination. In some scenarios, we can try it. Next, let's see how the two are combined to call each other.

NodeJS is used as the back end to call Python only when necessary

Python

In some special scenarios, such as complex and time-consuming data processing and calculation, we can use Python script to write, and then use the child process of Node to call Python script, which can improve efficiency. In the following code, we can use Node.Js API child_ process.spawn (command [,args][, options]) to call Python script:

Const spawn = require ('child_process'). Spawn
App.get ("process_data", (req, res) = > {
Spawn ('python3' ['script.py'])
})

Python script:

# script.py
DoSometing ()

If our Node script passes parameters to the Python script, how do we do it? The following code is shown:

Const spawn = require ('child_process'). Spawn
App.get ("process_data", (req, res) = > {
Const msg = "Hello"
Spawn ('python3' ['script.py', msg])
})

At this point, we need to change the script of Python to receive the parameters passed by NodeJS. The following code is shown:

import sys, json

Def main ():
Msg = sys.argv [1]
DoSometing (msg)

If _ _ name__ = ='_ _ main__':
Main ()

If we transfer complex data like JSON, we need to rewrite the way Node is written. Passed to Python in the form of a data stream, the sample code is as follows:

Const spawn = require ('child_process'). Spawn
Const py = spawn ('python3' ['script.py'])
Const data = {
Msg: "Hello"
}

Py.stdin.write (JSON.stringify (data))
// we have to send data as a string, so we are using JSON.stringify
Py.stdin.end ()

Next, let's change the Python script, receive the data stream transmitted by the node front-end, and perform the next logical processing.

import sys, json

Def main ():
Lines = sys.stdin.readlines ()
Data = json.loads (lines)
DoSometing (data ['msg'])

If _ _ name__ = ='_ _ main__':
Main ()

Node can not only transmit data to Python, but also receive data transmitted by Python. Next, let's rewrite the script related to Node. The sample code is as follows:

Const spawn = require ('child_process'). Spawn
Const py = spawn ('python3' ['cscript.py'])

Py.stdout.on ('data', function (res) {
Let data = JSON.parse (res.toString ())
Console.log (data)
})

Finally, adjust the Python script to transfer data to the Node front end. The sample code is as follows:

import sys

# You will have your own implementation of get data. In this case lets assume it returns a dict/json
Res = getData ()
Print (json.dumps (data))

Sys.stdout.flush ()
Label :
    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).