登录
首页 >  Golang >  Go问答

节点子进程:为什么 .kill() 不关闭“go run...”进程,但会关闭直接二进制文件?

来源:stackoverflow

时间:2024-03-08 14:45:28 261浏览 收藏

哈喽!今天心血来潮给大家带来了《节点子进程:为什么 .kill() 不关闭“go run...”进程,但会关闭直接二进制文件?》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我有一个长时间运行的 go 应用程序,我想从节点进程运行它。

package main

import (
    "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),这一切都可以正常工作。

我将得到以下输出:

running

exit
close
(process exits)

但是,如果我尝试使用 go run main.go 运行,我的输出是

running

exit
(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 pid
echo pid: $!

# wait for child to end
wait $!

然后执行 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.go
PID: 13679
13679

在节点代码中,您必须解析输出才能获取 pid。

到这里,我们也就讲完了《节点子进程:为什么 .kill() 不关闭“go run...”进程,但会关闭直接二进制文件?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>