javascript 在 nodejs 上启动新进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11876281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start new process on nodejs
提问by Mario
I'm working with node.js and I want to when there's a request to a url like ./calculate start a new process to make these complex calculations, and I want that process to continue even if the script which called it has finished. Is it possible?
我正在使用 node.js 并且我想当有一个像 ./calculate 这样的 url 的请求时启动一个新进程来进行这些复杂的计算,我希望这个进程继续,即使调用它的脚本已经完成。是否可以?
Thank you.
谢谢你。
采纳答案by Roman
You can use the natively provided child process facility: http://nodejs.org/api/child_process.html
您可以使用本机提供的子进程设施:http: //nodejs.org/api/child_process.html
And use the unix "nohup" command to keep the spawned process alive even if the parent process died.
并使用 unix "nohup" 命令使生成的进程保持活动状态,即使父进程死亡。
回答by 3on
There are different approach to this.
对此有不同的方法。
You could use https://github.com/pgriess/node-webworker.
您可以使用https://github.com/pgriess/node-webworker。
Or much better http://nodejs.org/docs/latest/api/cluster.html#cluster_cluster.
或者更好http://nodejs.org/docs/latest/api/cluster.html#cluster_cluster。
Those solutions are if you want to do a subprocess in Node, you could also simply spawn a new Node process and wait for the output (http://nodejs.org/api/all.html#all_child_process_spawn_command_args_options) but node-webworker is a wrapper around that solution and is a much cleaner.
这些解决方案是,如果你想在 Node 中做一个子进程,你也可以简单地生成一个新的 Node 进程并等待输出(http://nodejs.org/api/all.html#all_child_process_spawn_command_args_options)但是 node-webworker 是一个围绕该解决方案进行包装,并且更加清洁。
回答by 3on
So after your last comment I know understand better what you want to do.
因此,在您上次发表评论后,我更了解您想要做什么。
You want to spawn process not child process. The best and cleaner way to do this is to use some kind of demonizier to run them.
您想生成进程而不是子进程。最好和更干净的方法是使用某种恶魔化器来运行它们。
Have a look at forever:
https://github.com/nodejitsu/forever
https://github.com/nodejitsu/forever-monitor
看看永远:
https: //github.com/nodejitsu/forever
https://github.com/nodejitsu/forever-monitor
This could be the perfect tool for you. I've never used it programatically, just cli. But this is what I'd look into.
这可能是您的完美工具。我从来没有以编程方式使用过它,只是 cli。但这就是我要研究的。