SuncodeSoftware.SuperSDK.Python
5.0.0
dotnet add package SuncodeSoftware.SuperSDK.Python --version 5.0.0
NuGet\Install-Package SuncodeSoftware.SuperSDK.Python -Version 5.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SuncodeSoftware.SuperSDK.Python" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SuncodeSoftware.SuperSDK.Python" Version="5.0.0" />
<PackageReference Include="SuncodeSoftware.SuperSDK.Python" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SuncodeSoftware.SuperSDK.Python --version 5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SuncodeSoftware.SuperSDK.Python, 5.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SuncodeSoftware.SuperSDK.Python@5.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SuncodeSoftware.SuperSDK.Python&version=5.0.0
#tool nuget:?package=SuncodeSoftware.SuperSDK.Python&version=5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SuperSDK.Python
SuperSDK Python 进程桥接库,适用于 .NET 9 / Avalonia 桌面应用。
无需额外 Python 包(仅标准库),new GzPythonWrapper() 自动完成进程启动与连接。
安装
dotnet add package SuncodeSoftware.SuperSDK.Python
快速上手
1. 初始化(自动完成)
using SuperSDK.Python;
var py = new GzPythonWrapper();
// 构造函数自动以 python3 启动桥接进程,首次 CallAsync 前完成连接
2. 调用 Python 方法
// CallAsync(方法名, new { P1 = 参数1, P2 = 参数2, Timeout = 超时(默认5000ms) })
var result = await py.CallAsync("process", new { P1 = "input" });
if (result.Success)
// Python 已接受请求并在后台处理,结果通过 OnProgress 事件回传
Console.WriteLine($"请求已提交,ID: {result.RequestId}");
else
Console.WriteLine(result.Error); // 连接失败 / 方法未注册等
// C# 一行包装
public Task<GzResult> ProcessAsync(string input)
=> _py.CallAsync("process", new { P1 = input });
GzResult 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
Success |
bool |
Python 是否已成功接受请求 |
RequestId |
string |
本次请求的唯一码,可与后续 progress 事件关联 |
Error |
string? |
失败时的错误信息(连接未就绪、方法未注册等) |
3. 接收 Python 推送的事件
Python 通过 publish() 推送两类消息:
- 系统消息(
GzEventType.System):如心跳,每 10 秒自动推送 - 回复消息(
GzEventType.Reply):携带RequestId,关联到某次CallAsync
推荐:继承重写虚方法(心跳和 progress 已在构造函数中内部订阅)
public class MyPythonBridge : GzPythonWrapper
{
protected override void OnProgress(GzEventMessage msg)
{
// msg.RequestId 对应发起请求时的 GzResult.RequestId
Console.WriteLine($"请求 {msg.RequestId} 进度: {msg.Data}");
}
protected override void OnHeartbeat(GzEventMessage msg)
{
Console.WriteLine($"心跳 {msg.Data}");
}
}
或者:直接订阅其他自定义事件
py.Subscribe("my_event", msg =>
Console.WriteLine($"收到事件: {msg.Data}"));
GzEventMessage 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
Id |
string |
事件自身的唯一码 |
Type |
GzEventType |
System / Reply |
Event |
string |
事件名 |
RequestId |
string? |
关联的请求 ID(Reply 类型) |
Data |
string |
事件数据(字符串) |
4. 释放
await py.ReleaseAsync(); // 断开连接,终止 Python 进程
添加自定义方法(最小改动)
Python 侧(在 gz_methods.py 中添加一个函数):
@rpc_method
def process(params):
req_id = params.get("__request_id") # 请求唯一码(自动注入)
publish(data="处理中 50%", request_id=req_id) # 推送 progress 事件
publish(data="处理完成", request_id=req_id)
# 返回值被忽略,所有结果通过 publish 推送
C# 侧(一行包装 + 重写回调):
// 发起请求
public Task<GzResult> ProcessAsync(string input)
=> _py.CallAsync("process", new { P1 = input });
// 接收进度(继承方式,见上方第 3 节)
protected override void OnProgress(GzEventMessage msg)
=> Console.WriteLine($"[{msg.RequestId}] {msg.Data}");
API 参考
| 方法 | 说明 |
|---|---|
new GzPythonWrapper() |
自动启动 python3 进程 |
CallAsync(method, args) |
调用 Python RPC 方法,返回 GzResult |
Subscribe(eventName, handler) |
订阅 Python 推送的事件 |
Unsubscribe(eventName) |
取消某事件的全部订阅 |
ReleaseAsync() |
断开连接,终止进程 |
IsRunning |
Python 进程是否正在运行 |
要求
- .NET 9+
- Python 3.8+(推荐 3.14,仅使用标准库,无需 pip 安装任何包)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- SuncodeSoftware.SuperSDK.Core (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.0.0 | 0 | 3/13/2026 |
新增Python包