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:
2026-04-14 13:49:33 +08:00
parent b390effd8e
commit 39517dc5fc
4 changed files with 48 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python"))
import dataclasses
@@ -13,14 +14,17 @@ from gobridge import expose, call_go, run, worker_id, worker_count
# 其余 worker 跳过,避免端口冲突 / 重复连接。
print(f"[worker {worker_id}/{worker_count}] started", flush=True)
if worker_id == 0:
def _init_shared_resource():
# 示例:此处可启动 WebSocket 客户端、监听 TCP 端口等
print(f"[worker {worker_id}] shared resource initialized", flush=True)
threading.Thread(target=_init_shared_resource, daemon=True).start()
# ── 基础类型 ─────────────────────────────────────────────────────────────────
@expose
def add(a: int, b: int) -> int:
return a + b
@@ -48,6 +52,7 @@ def double_stream(numbers: Iterator[int]) -> Iterator[int]:
# ── structdataclass / dict类型 ───────────────────────────────────────────
@dataclasses.dataclass
class User:
id: int
@@ -95,6 +100,7 @@ def process_users(users: Iterator[dict]) -> Iterator[dict]:
# ── Server 全双工示例 ────────────────────────────────────────────────────────
@expose
def compute_with_go_mul(a: int, b: int) -> int:
"""示例1call_go[int] 指定返回类型"""
@@ -137,4 +143,7 @@ def get_user_via_go(uid: int) -> dict:
return dataclasses.asdict(user)
run()
if __name__ == "__main__":
run()
print("worker_id", worker_id)
print("worker_count", worker_count)