xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js child_process spawn All In One

Node.js child_process spawn All In One

Node.js 多线程

// const { spawn } = require('child_process');
const { spawn } = require('node:child_process');

// $ ls -al /usr 等价于
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

https://nodejs.org/api/child_process.html

How to run Python script code in Node.js?

如何在 Node.js 中运行 Python 脚本代码?

// const { spawn } = require('child_process');
const { spawn } = require('node:child_process');

// async API
async function generateJsonFile(req, res) {
  // console.log(1)
  let pyData;
  // python3 ./py-script.py Node.js ✅
  const py = spawn('python3', ['./py-script.py', 'Node.js']);
  // py3 ./py-script.py Node.js ❌ py3 alias not work ❌
  // const py = spawn('py3', ['./py-script.py', 'Node.js']);
  // const py = spawn('py3', ['./py-script.py', 'Node.js'], {
  //   stdio: 'inherit',
  //   // shell: true,
  // });
  py.stdout.on('data', data => {
    console.log(`✅buffer =`, data);
    // DataView
    pyData = data.toString();
    console.log('✅Data received from python script =', `❓` + pyData + `❓`);
  });
  py.stderr.on('data', (data) => {
    console.error(`❌stderr: ${data}`);
  });
  py.on('close', (code) => {
    console.log('👻 Data received from python script =', code);
    // console.log('👻 Data received from python script =', pyData);
    // res.send(pyData);
  });
  // console.log(2)
}

(async () => {
  await generateJsonFile();
})();

import sys

print('🐍 py3 script is running!')

input = sys.argv[1]
print('input =', input)

data_to_pass_back = 'Send this to node process'
output = data_to_pass_back
print('output =', output)

# ???
sys.stdout.flush()

image

demos


(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

Python 3

sys.stdout.flush()

https://www.python.org/doc/

import sys
import time

for i in range(10):
  print(i, end = ' ')
  time.sleep(0.1)
# 没有使用 flush,for 循环中所有的 print 全部执行完成后,一次行输出结果 ❌

Python3 program demonstrating working of flush during output and usage of sys.stdout.flush() function

import sys
import time

for i in range(10):
  print(i, end = ' ')
  sys.stdout.flush()
  time.sleep(0.1)
# 使用 flush 后,for 循环中每一次 print 执行完成后,就立即输出结果 ✅,不用等待全部执行完成再输出

https://www.geeksforgeeks.org/python-sys-stdout-flush/

import sys
import time

for i in range(10):
  print(i, end = ' ')
  time.sleep(0.1)
# 没有使用 flush,for 循环中所有的 print 全部执行完成后,一次行输出结果 ❌

time.sleep(3)
print('\n')

for i in range(10):
  print(i, end = ' ')
  sys.stdout.flush()
  time.sleep(0.1)
# 使用 flush 后,for 循环中每一次 print 执行完成后,就立即输出结果 ✅,不用等待全部执行完成再输出

sys.stdout.flush() 用于强制程序刷新输出缓冲区。
当您想要确保输出缓冲区中的所有内容立即打印到控制台或文件时,这非常有用。

https://stackoverflow.com/questions/10019456/usage-of-sys-stdout-flush-method

https://realpython.com/installing-python/

https://www.runoob.com/python3/python3-tutorial.html

refs

https://www.npmjs.com/package/cross-spawn

https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/

https://www.freecodecamp.org/chinese/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/

https://www.geeksforgeeks.org/run-python-script-node-js-using-child-process-spawn-method/



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-09-13 19:31  xgqfrms  阅读(22)  评论(0编辑  收藏  举报