使用 Node.js 调用child_process与从 C 调用子进程并创建C++绑定以从 node.js 调用

Calling child_process with Node.js vs calling child process from C and creating a C++ bind to call from node.js

本文关键字:调用 js C++ 创建 绑定 node child Node process 使用 与从      更新时间:2023-10-16

我想调用pdftotext来提取100.000个文件的内容(我需要快(,那么,这两种实现中哪一种是最快的?

实施 1:

从 node.js 为每个提取创建一个child_process:

export default (file) => new Promise((resove, reject) => {
const command = 'pdftotext'
const args = ['-layout', '-enc', 'UTF-8', file, '-']
const process = spawn(command, args)
const stdout = []
const stderr = []
process.stdout.on('data', (data) => {
stdout.push(data)
})
process.stderr.on('data', (data) => {
stderr.push(data)
})
process.on('error', (error) => {
if (error.code === 'ENOENT')
error.message = 'pdftotext is not installed, so will be unable to extract the file content'
reject(error)
})
process.on('close', () => {
if (stderr.length)
return reject(stderr.map(Error))
resolve(stdout.join())
})
}

实施 2:

从 C 创建child_process,并创建要从节点调用的C++绑定.js

--没有代码,因为我还在学习如何做 --

最有可能的是,进程调用代码会对性能产生不必要的影响,文档处理的速度取决于pdftotext实现和磁盘io。所以我想没有必要费心编写自定义进程启动器。