Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

当涉及到 web开发时,javascript是无人能及的存在,但是有的时候我们需高要求的工作,比如解析一大堆数据,这样pyhton就是个更好的选择,但这仅仅是我们网站的一个功能而已,仅因为这个我们就需要转到python吗,也许不用。

如果我们可以大部分使用 Node JS, 在必要情况下使用python呢?

碉堡了, 对吧,如有需要,我们可以使用child process 去运行一个python脚本

1
2
3
4
5
6

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

1
2
# script.py
doSometing()

我们也可以传参

1
2
3
4
5
6
7

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

在 python中我们必须通过 import the sys 才能接收数据

1
2
3
4
5
6
7
8
9
10

import sys, json

def main():
msg = sys.argv[1]
doSometing(msg)

if __name__ == '__main__':
main()

我们也可以通过stream代替 生成python进程传参

1
2
3
4
5
6
7
8
9
10

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()

1
2
3
4
5
6
7
8
9
import sys, json

def main():
lines = sys.stdin.readlines()
data = json.loads(lines)
doSometing(data['msg'])

if __name__ == '__main__':
main()

最后通过python 回调函数把数据传到node

1
2
3
4
5
6
7
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)
})
1
2
3
4
5
6
7
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()

翻译自 https://dev.to/0shuvo0/python-javascript--1nd6

评论