JavaScript doesn't allow multithreading. So I'm wondering if I should use
const node = require('child_process').spawn('node', ['-e', code]);
Where code
is the lengthy operation's code I wish to run.
I've tried it and it does work, the main JS program doesn't remain blocked.
I can also use node.kill()
after a timeout to stop the process.
No, you should not use child_process
for node processes (unless you really need the OS to manage a separate process for some reason).
To run CPU-intensive JavaScript code without blocking, use worker threads . They have less overhead, simpler communication, and may share memory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.