简体   繁体   English

在nodejs上启动新进程

[英]Start new process on nodejs

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. 我正在使用node.js,我希望当有一个像./calculate这样的URL的请求启动一个新进程来进行这些复杂的计算时,即使调用它的脚本已经完成,我希望该进程继续。 Is it possible? 可能吗?

Thank you. 谢谢。

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”命令使生成的进程保持活动状态,即使父进程已经死亡。

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是一个围绕该解决方案包装,更清洁。

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
https://github.com/nodejitsu/forever-monitor https://github.com/nodejitsu/forever-monitor

This could be the perfect tool for you. 这可能是您的完美工具。 I've never used it programatically , just cli. 我从来没有以编程方式使用它,只是cli。 But this is what I'd look into. 但这就是我要研究的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM