Skip to content

大块数据:文件、图片、二进制 ​

通道适合传「小、结构化、要立刻处理」的数据。反过来,图片、PDF、视频、导出文件、几 MB 的日志这类东西,走通道要先 base64(体积 +33%)、再塞进 JSON、两端各复制一遍。Axon 的做法是:Python 把字节登记成一个 URL,浏览器自己去取,数据完全不经过通道。

走通道走 axon:// URL
1 MB 二进制base64 后 1.33 MB,两端各一次复制原始 1 MB,Chromium 直接读
5 MB 图片卡顿、内存翻倍正常显示,可流式
视频基本不可行<video src> 可用(拖动进度条暂不支持)
前端写法先 await 拿数据再转 blob直接把 URL 给 src / fetch

Python 侧 ​

controller/Report.py
python
from axon import module axon.AxonAssetAxonAsset, AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。
AxonController
@AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。
AxonController
("MainView")
class class ReportReport: def chart_url(self) -> str: png = build_chart_png() # 你自己的 bytes return module axon.AxonAssetAxonAsset.from_bytes(data: bytes | bytearray | memoryview, name: str = 'blob', mime: str | None = None) -> strfrom_bytes(png, name="chart.png") # → axon://asset/<token>/chart.png def export_url(self) -> str: return module axon.AxonAssetAxonAsset.from_file(path: str | pathlib.Path, name: str | None = None, mime: str | None = None) -> strfrom_file("/tmp/report.pdf") # 文件不进内存 def drop(self, url: str) -> bool: return module axon.AxonAssetAxonAsset.release(url_or_token: str) -> boolrelease(url) # 用完释放,不释放也能用到进程结束

三个函数:

函数说明
AxonAsset.from_bytes(data, name="blob", mime=None)一段 bytes → URL;mime 默认按文件名猜
AxonAsset.from_file(path, name=None)本地文件 → URL(不读进内存,请求时才打开)
AxonAsset.release(url) / clear() / count()释放 / 全清 / 当前数量

只保留类方法:AxonAsset.from_bytes(...) / AxonAsset.from_file(...)(早期版本还有模块级别名,已删除)。

URL 形如 axon://asset/<token>/<文件名>:token 决定内容,文件名只影响 Content-Type 和下载时的默认名。

JS 侧 ​

拿到 URL 之后就是普通的 Web 写法,不用引任何 Axon 的 API:

js
const const url: anyurl = await 
const Axon: {
    DemoController: {
        add(a: number, b: number): Promise<number>;
        asset_count(): Promise<number>;
        blob(size?: number): Promise<string>;
        broadcast(text: string): Promise<Array<any>>;
        call_js_mirror(payload: any): Promise<any>;
        cross_broadcast(text?: string): Promise<Record<string, any>>;
        cross_call(text?: string): Promise<any>;
        cross_close(): Promise<Array<string>>;
        cross_emit(text?: string): Promise<Array<string>>;
        cross_open(): Promise<Array<string>>;
        delayed(msg: string, ms?: number): Promise<string>;
        ... 13 more ...;
        counter: number;
    };
}

Axon 的 JS 侧语法糖。

ts
await Axon.Users.add(1, 2)          // 调 Python 方法, 位置参数直接摊开
await Axon.Users.slow([1000], {}, 300)  // 需要 kwargs / 超时时用打包写法
await Axon.Users.count              // 读 Python 属性 (getVar)
Axon.Users.count = 3                // 写 Python 属性 (setVar, fire-and-forget)

属性读是惰性的: 只有真正 await / .then / .catch / .finally 时才发请求。 取到的 dispatcher 本身是函数, 同时挂了 then/catch/finally, 让 Axon.Users.count 既能当 Promise 用, 又能当方法调用。

Axon
.Report.chart_url();
var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.ParentNode.querySelector<Element>(selectors: string): Element | null (+4 overloads)

Returns the first element that is a descendant of node that matches selectors.

MDN Reference

querySelector
("#chart").src = const url: anyurl; // <img src="axon://asset/…">
js
const const url: anyurl = await 
const Axon: {
    DemoController: {
        add(a: number, b: number): Promise<number>;
        asset_count(): Promise<number>;
        blob(size?: number): Promise<string>;
        broadcast(text: string): Promise<Array<any>>;
        call_js_mirror(payload: any): Promise<any>;
        cross_broadcast(text?: string): Promise<Record<string, any>>;
        cross_call(text?: string): Promise<any>;
        cross_close(): Promise<Array<string>>;
        cross_emit(text?: string): Promise<Array<string>>;
        cross_open(): Promise<Array<string>>;
        delayed(msg: string, ms?: number): Promise<string>;
        ... 13 more ...;
        counter: number;
    };
}

Axon 的 JS 侧语法糖。

ts
await Axon.Users.add(1, 2)          // 调 Python 方法, 位置参数直接摊开
await Axon.Users.slow([1000], {}, 300)  // 需要 kwargs / 超时时用打包写法
await Axon.Users.count              // 读 Python 属性 (getVar)
Axon.Users.count = 3                // 写 Python 属性 (setVar, fire-and-forget)

属性读是惰性的: 只有真正 await / .then / .catch / .finally 时才发请求。 取到的 dispatcher 本身是函数, 同时挂了 then/catch/finally, 让 Axon.Users.count 既能当 Promise 用, 又能当方法调用。

Axon
.Report.export_url();
const const buf: ArrayBufferbuf = await (await function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>fetch(const url: anyurl)).Body.arrayBuffer(): Promise<ArrayBuffer>arrayBuffer(); // 1 MB → 几毫秒 var console: Consoleconsole.Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(const buf: ArrayBufferbuf.ArrayBuffer.byteLength: number

Read-only. The length of the ArrayBuffer (in bytes).

byteLength
);
js
const const url: anyurl = await 
const Axon: {
    DemoController: {
        add(a: number, b: number): Promise<number>;
        asset_count(): Promise<number>;
        blob(size?: number): Promise<string>;
        broadcast(text: string): Promise<Array<any>>;
        call_js_mirror(payload: any): Promise<any>;
        cross_broadcast(text?: string): Promise<Record<string, any>>;
        cross_call(text?: string): Promise<any>;
        cross_close(): Promise<Array<string>>;
        cross_emit(text?: string): Promise<Array<string>>;
        cross_open(): Promise<Array<string>>;
        delayed(msg: string, ms?: number): Promise<string>;
        ... 13 more ...;
        counter: number;
    };
}

Axon 的 JS 侧语法糖。

ts
await Axon.Users.add(1, 2)          // 调 Python 方法, 位置参数直接摊开
await Axon.Users.slow([1000], {}, 300)  // 需要 kwargs / 超时时用打包写法
await Axon.Users.count              // 读 Python 属性 (getVar)
Axon.Users.count = 3                // 写 Python 属性 (setVar, fire-and-forget)

属性读是惰性的: 只有真正 await / .then / .catch / .finally 时才发请求。 取到的 dispatcher 本身是函数, 同时挂了 then/catch/finally, 让 Axon.Users.count 既能当 Promise 用, 又能当方法调用。

Axon
.Report.export_url();
const const a: HTMLAnchorElementa = var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.Document.createElement<"a">(tagName: "a", options?: ElementCreationOptions): HTMLAnchorElement (+2 overloads)

In an HTML document, the document.createElement() method creates the HTML element specified by localName, or an HTMLUnknownElement if localName isn't recognized.

MDN Reference

createElement
("a");
const a: HTMLAnchorElementa.HTMLHyperlinkElementUtils.href: string

Returns the hyperlink's URL.

Can be set, to change the URL.

MDN Reference

href
= const url: anyurl;
const a: HTMLAnchorElementa.HTMLAnchorElement.download: string

The HTMLAnchorElement.download property is a string indicating that the linked resource is intended to be downloaded rather than displayed in the browser. The value, if any, specifies the default file name for use in labeling the resource in a local file system. If the name is not a valid file name in the underlying OS, the browser will adjust it.

MDN Reference

download
= "report.pdf"; // 文件名以 URL 末尾为准
const a: HTMLAnchorElementa.HTMLElement.click(): void

The HTMLElement.click() method simulates a mouse click on an element. When called on an element, the element's click event is fired (unless its disabled attribute is set).

MDN Reference

click
();

`

Python 生成 2 MB 二进制登记成 URL,前端 fetch 回来:上面这条消息流里能看到 URL、字节数和耗时——数据没有经过通道。

要注意的 ​

什么时候释放

不用管,默认自动回收:空闲 10 分钟没被取用的资产会清掉;字节总量超过 256 MB 时按「最久未用」淘汰 (from_file() 的资产不占内存,只留一个路径)。要调:

AxonAsset.configure(ttl=300, budget=64 << 20)   # 秒 / 字节;<=0 表示不限
AxonAsset.release(url)                          # 确实想立刻腾出来

支持 Range(视频能拖)

axon:// 支持分段请求:媒体栈发 Range: bytes=… 时,框架回 206 Partial Content + 正确的 Content-Range,所以视频拖动进度条、边下边播、断点续传都能用。两点边界:

  • 多区间(Range: bytes=0-99,200-299)不实现 multipart;这种请求按整份内容回 200(RFC 允许)。
  • 越界区间(起点 ≥ 文件长度)直接拒掉,回 400——这是刻意的:放任 Qt 去 seek 越界位置会让渲染进程直接 abort。

什么时候还是走通道

结构化的小数据(一屏列表、几十 KB 的 JSON)走通道更简单——不用管生命周期。只有「大」或「二进制」才值得用 URL。

和压缩的关系 ​

跨端传 字符串 时,超过 256 KB 会自动 gzip(见调用 Python 方法里的接口说明),所以文本类大 payload 不用你操心;bytes 走 axon://,两者互补。