php小编鱼仔在这篇文章中将为大家解答一个常见的问题:为什么在使用.kill()命令关闭进程时,可以关闭直接二进制文件,但无法关闭”go run…”进程?对于这个问题,我们需要了解一下节点子进程的工作原理。节点子进程是由go run命令启动的,它运行的是一个临时文件,而不是直接的二进制文件。这就导致了在使用.kill()命令关闭进程时,直接二进制文件可以被正确关闭,但临时文件却无法被关闭。本文将详细解析这个问题并给出解决方法。
问题内容
我有一个长时间运行的 go 应用程序,我想从节点进程运行它。
package mainimport ( "fmt" "net/http")func main() { fmt.println("running") http.listenandserve(":80", nil)}
登录后复制
我在节点中设置了我的子进程,例如:
async function application(){ //const myprocess = exec("go run main.go"); const myprocess = exec("./main"); myprocess.stdout.on('data', (chunk) => { console.log(chunk); }) return new promise(res => { settimeout(() => { myprocess.on("close", () => { console.log("close") res(); }); myprocess.on("exit", () => { console.log("exit") }) myprocess.kill(); }, 1000); })}
登录后复制
如果我直接运行编译的二进制文件(./main),这一切都可以正常工作。
我将得到以下输出:
runningexitclose(process exits)
登录后复制
但是,如果我尝试使用 go run main.go 运行,我的输出是
runningexit(process continues)
登录后复制
具体来说,这里发生了什么,go run 进程无法正常关闭?
我的理解是,这将归结为“关闭”和“退出”事件之间的区别。
来自文档:
The 'close' event is emitted after a process has ended and the stdio streams of a child process have been closed. This is distinct from the 'exit' event, since multiple processes might share the same stdio streams. The 'close' event will always emit after 'exit' was already emitted, or 'error' if the child failed to spawn.
登录后复制
好吧,那么也许 go run 让 stdio 流保持打开状态?这就是我的理解不清楚的地方。
对于我正在解决的问题,仅使用二进制文件就可以正常工作。
但是我很好奇,如果我确实想使用 go run 进程 – 我该如何正确关闭它?
在此重现:github。
解决方法
go run 命令构建可执行文件,然后在后台运行可执行文件以等待它。这意味着 go run 的进程 id 与可执行文件的 pid 不同。
您可以使用 -exec 告诉 go run 运行另一个程序(不是 go)。例如,您可以编写run.sh
#!/bin/bash# run executable in background$1&# $! is child pidecho pid: $!# wait for child to endwait $!
登录后复制
然后执行 go run -exec /path/to/run.sh main.go使用以下 main.go 尝试此操作:包主要
import ( "fmt" "os")func main() { fmt.println(os.getpid())}
登录后复制
得到:
$ go run -exec /tmp/run.sh /tmp/main.goPID: 1367913679
登录后复制
在节点代码中,您必须解析输出才能获取 pid。
以上就是节点子进程:为什么 .kill() 不关闭“go run…”进程,但会关闭直接二进制文件?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2484352.html