Home > Backend Development > Golang > Node subprocess: Why doesn't .kill() close the 'go run...' process but does the direct binary?

Node subprocess: Why doesn't .kill() close the 'go run...' process but does the direct binary?

WBOY
Release: 2024-02-09 19:10:08
forward
1149 people have browsed it

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

php小编鱼仔在这篇文章中将为大家解答一个常见的问题:为什么在使用.kill()命令关闭进程时,可以关闭直接二进制文件,但无法关闭"go run..."进程?对于这个问题,我们需要了解一下节点子进程的工作原理。节点子进程是由go run命令启动的,它运行的是一个临时文件,而不是直接的二进制文件。这就导致了在使用.kill()命令关闭进程时,直接二进制文件可以被正确关闭,但临时文件却无法被关闭。本文将详细解析这个问题并给出解决方法。

问题内容

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

package main

import (
    "fmt"
    "net/http"
)

func main() {
    fmt.println("running")
    http.listenandserve(":80", nil)
}
Copy after login

我在节点中设置了我的子进程,例如:

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); 
    
    })
}
Copy after login

如果我直接运行编译的二进制文件(./main),这一切都可以正常工作。

我将得到以下输出:

running

exit
close
(process exits)
Copy after login

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

running

exit
(process continues)
Copy after login

具体来说,这里发生了什么,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.
Copy after login

好吧,那么也许 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 $!
Copy after login

然后执行 go run -exec /path/to/run.sh main.go 使用以下 main.go 尝试此操作: 包主要

import (
    "fmt"
    "os"
)

func main() {
    fmt.println(os.getpid())
}
Copy after login

得到:

$ go run -exec /tmp/run.sh /tmp/main.go
PID: 13679
13679
Copy after login

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

The above is the detailed content of Node subprocess: Why doesn't .kill() close the 'go run...' process but does the direct binary?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template