feat: 通过匿名管道实现父进程死亡检测,替代轮询方案
- worker.go: 为每个子进程创建 death pipe,Go 持有写端 - __init__.py: run() 中监听管道读端 EOF,Go 退出时立即终止子进程 - 解决 Ctrl+C / panic / SIGKILL 等场景下 Python worker 变成孤儿进程的问题 - Python 包版本升至 0.1.2
This commit is contained in:
@@ -385,6 +385,17 @@ def run():
|
||||
server.bind(sock_path)
|
||||
server.listen(64)
|
||||
|
||||
# Death pipe:Go 持有写端,本进程阻塞读端。
|
||||
# Go 进程消亡(任何原因:Ctrl+C、panic、SIGKILL)时写端自动关闭,read() 立即返回 EOF。
|
||||
death_fd = int(os.environ.get("GOBRIDGE_DEATH_FD", "0"))
|
||||
if death_fd:
|
||||
def _watch_death_pipe():
|
||||
with os.fdopen(death_fd, "rb") as f:
|
||||
f.read(1) # 阻塞直到 Go 关闭写端(EOF)
|
||||
server.close()
|
||||
os._exit(0)
|
||||
threading.Thread(target=_watch_death_pipe, daemon=True).start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "gobridge"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
description = "Python 端库,配合 Go 侧 gobridge 使用"
|
||||
requires-python = ">=3.10"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user