From 07e9239ac55bf70c633481cc6d677186f976bbd3 Mon Sep 17 00:00:00 2001 From: what Date: Mon, 13 Apr 2026 17:13:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=E8=A3=85=E9=A5=B0?= =?UTF-8?q?=E5=99=A8=20gobridge=20=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BA=20e?= =?UTF-8?q?xpose=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=20pyproject.toml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 ++++++++++---------- example/worker.py | 20 ++++++++++---------- python/gobridge/__init__.py | 12 ++++++------ python/pyproject.toml | 12 ++++++++++++ 4 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 python/pyproject.toml diff --git a/README.md b/README.md index 795c088..63152d2 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Python 端直接复制 `python/gobridge/` 目录到项目中,无需安装依 from gobridge import gobridge, run from typing import Iterator -@gobridge +@expose def add(a: int, b: int) -> int: return a + b @@ -61,15 +61,15 @@ result, err := gobridge.Invoke[[]User](ctx, pool, "enrich_users", users) ```python # Python -@gobridge +@expose def add(a: int, b: int) -> int: return a + b -@gobridge +@expose def get_user(uid: int) -> dict: return {"id": uid, "name": f"user_{uid}", "score": uid * 1.5} -@gobridge +@expose def enrich_users(users: list) -> list: for u in users: u["level"] = "gold" if u["score"] >= 10 else "silver" @@ -95,12 +95,12 @@ for u := range userCh { ```python # Python -@gobridge +@expose def range_gen(start: int, stop: int) -> Iterator[int]: for i in range(start, stop): yield i -@gobridge +@expose def gen_users(count: int) -> Iterator[dict]: for i in range(1, count + 1): yield {"id": i, "name": f"user_{i}", "score": float(i * 3)} @@ -125,7 +125,7 @@ fmt.Println(total) // 15 ```python # Python -@gobridge +@expose def sum_stream(numbers: Iterator[int]) -> int: return sum(numbers) ``` @@ -151,7 +151,7 @@ for u := range outCh { ```python # Python -@gobridge +@expose def process_users(users: Iterator[dict]) -> Iterator[dict]: for u in users: yield {"id": u["id"], "name": u["name"].upper(), "score": u["score"] * 2} @@ -170,7 +170,7 @@ result, err := gobridge.Invoke[int](ctx, pool, "slow_compute", 1000000) ``` ```python -@gobridge +@expose def slow_compute(n: int) -> int: total = 0 for i in range(n): @@ -360,7 +360,7 @@ gobridge = { git = "https://git.fsdpf.net/go/gobridge.git", subdirectory = "pyth │ │ _dispatch() │ → 执行线程抛 InterruptedError│ │ │ │ │ │ │ │ │ │ ┌──────▼──────┐ │ │ │ - │ │ │ @gobridge fn│ │ │ │ + │ │ │ @expose fn│ │ │ │ │ │ │ │ │ │ │ │ │ │ 普通函数 │ │ │ │ │ │ │ return val ──────────────► result/error │ │ diff --git a/example/worker.py b/example/worker.py index 90baefa..5626c90 100644 --- a/example/worker.py +++ b/example/worker.py @@ -2,29 +2,29 @@ import sys import os sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python")) -from gobridge import gobridge, run +from gobridge import expose, run from typing import Iterator -@gobridge +@expose def add(a: int, b: int) -> int: return a + b -@gobridge +@expose def range_gen(start: int, stop: int) -> Iterator[int]: """流式输出:对应 Go 侧 Invoke[chan int]""" for i in range(start, stop): yield i -@gobridge +@expose def sum_stream(numbers: Iterator[int]) -> int: """流式输入:对应 Go 侧传入 chan int 参数""" return sum(numbers) -@gobridge +@expose def double_stream(numbers: Iterator[int]) -> Iterator[int]: """双向流:输入每个数,yield 其平方,对应 Go 侧 Invoke[chan int](c, ctx, "double_stream", inputChan)""" for n in numbers: @@ -33,19 +33,19 @@ def double_stream(numbers: Iterator[int]) -> Iterator[int]: # ── struct(dict)类型 ────────────────────────────────────────────────────── -@gobridge +@expose def get_user(uid: int) -> dict: """普通调用:返回一个 struct(Go 对应 User)""" return {"id": uid, "name": f"user_{uid}", "score": uid * 1.5} -@gobridge +@expose def total_score(users: list) -> float: """slice 输入:接收 []User,返回总分""" return sum(u["score"] for u in users) -@gobridge +@expose def enrich_users(users: list) -> list: """slice 输入输出:为每个 user 追加 level 字段""" result = [] @@ -58,14 +58,14 @@ def enrich_users(users: list) -> list: # ── struct/slice 流式组合 ──────────────────────────────────────────────────── -@gobridge +@expose def gen_users(count: int) -> Iterator[dict]: """流式输出 struct:yield 多个 User,对应 Go 侧 Invoke[chan User]""" for i in range(1, count + 1): yield {"id": i, "name": f"user_{i}", "score": float(i * 3)} -@gobridge +@expose def process_users(users: Iterator[dict]) -> Iterator[dict]: """双向流 struct:输入流式 User,yield 处理后的 User""" for u in users: diff --git a/python/gobridge/__init__.py b/python/gobridge/__init__.py index bf584ad..8e66861 100644 --- a/python/gobridge/__init__.py +++ b/python/gobridge/__init__.py @@ -3,24 +3,24 @@ gobridge - Python 端库,配合 Go 侧 gobridge 使用 用法:: - from gobridge import gobridge, run + from gobridge import expose, run from typing import Iterator - @gobridge + @expose def add(a: int, b: int) -> int: return a + b - @gobridge + @expose def range_gen(start: int, stop: int) -> Iterator[int]: for i in range(start, stop): yield i # 对应 Go 侧 Invoke[chan int] - @gobridge + @expose def sum_stream(numbers: Iterator[int]) -> int: return sum(numbers) # 对应 Go 侧传入 chan int 参数 # ctx 取消时,框架自动向该线程注入 InterruptedError,无需在函数中检查 - @gobridge + @expose def slow_compute(n: int) -> int: total = 0 for i in range(n): @@ -43,7 +43,7 @@ import threading _exposed: dict = {} -def gobridge(fn): +def expose(fn): """装饰器:将函数暴露给 Go 侧调用""" _exposed[fn.__name__] = fn return fn diff --git a/python/pyproject.toml b/python/pyproject.toml new file mode 100644 index 0000000..0cff467 --- /dev/null +++ b/python/pyproject.toml @@ -0,0 +1,12 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "gobridge" +version = "0.1.0" +description = "Python 端库,配合 Go 侧 gobridge 使用" +requires-python = ">=3.10" + +[tool.hatch.build.targets.wheel] +packages = ["gobridge"]