English
Reading and writing Python attributes
JS → PY
Axon.Xxx.name in JS is not a plain property access — it is a cross-process read. So reads need await, writes do not wait.
Python side: just an attribute
controller.py
python
# controller.py
from axon import 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 DemoControllerDemoController:
counter: intcounter: int = 0 # class or instance attribute, both work
def get_counter(self) -> int:
return self.counter: intcounterJS side: await the read, assign the write
app.js
js
import { 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 侧语法糖。
tsawait 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, function AxonInit(): Promise<IAxonInitResult>初始化 Axon JS 侧。
返回状态而不是抛错: 普通浏览器里调用会得到 {connected:false, ready:false},
页面可以据此退化成纯静态说明书。
AxonInit } from "axon-gui";
await function AxonInit(): Promise<IAxonInitResult>初始化 Axon JS 侧。
返回状态而不是抛错: 普通浏览器里调用会得到 {connected:false, ready:false},
页面可以据此退化成纯静态说明书。
AxonInit();
// read: one round trip, so it must be awaited
const const before: numberbefore = 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 侧语法糖。
tsawait 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.type 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>;
fail(reason?: string): Promise<null>;
... 12 more ...;
counter: number;
}
DemoController.counter: number属性(读要 await,写走 setVar)
counter; // 0
// write: send and forget, no waiting for Python
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 侧语法糖。
tsawait 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.type 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>;
fail(reason?: string): Promise<null>;
... 12 more ...;
counter: number;
}
DemoController.counter: number属性(读要 await,写走 setVar)
counter = 42;
// read again after a moment to confirm Python really changed
const const after: numberafter = 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 侧语法糖。
tsawait 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.type 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>;
fail(reason?: string): Promise<null>;
... 12 more ...;
counter: number;
}
DemoController.function get_counter(): Promise<number>get_counter(); // 42Why reading is asynchronous
Axon.DemoController.counter gives you a "value will arrive later" object in JS: the framework has to send the request to Python and wait for the current value.
console.log(Axon.DemoController.counter)shows an object, not the value;await Axon.DemoController.countergives you the actual number.
When a write takes effect
A write returns as soon as it is sent. It reaches Python in the same order as any later calls, so the usual pattern is:
app.js
js
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 侧语法糖。
tsawait 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.type 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>;
fail(reason?: string): Promise<null>;
... 12 more ...;
counter: number;
}
DemoController.counter: number属性(读要 await,写走 setVar)
counter = 42;
const const now: numbernow = 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 侧语法糖。
tsawait 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.type 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>;
fail(reason?: string): Promise<null>;
... 12 more ...;
counter: number;
}
DemoController.function get_counter(): Promise<number>get_counter(); // always 42If you need proof the write succeeded (say Python might reject it), call an explicit method and treat its return value as the receipt.
Hover to see the types
getVar<T>() returns T directly (reading a property is still a round trip).
ts
import { const AxonCall: {
init(): Promise<void>;
__call__<K extends SmartPYControllerName, F extends SmartPYControllerPropName<K>, T = unknown>({ controller_id, prop, args, kwargs, timeout, type, }: {
controller_id: K;
prop: F;
args?: any[];
kwargs?: { [key in string]: any; };
timeout?: number;
type: JSCallType;
}): Promise<T>;
call<T = unknown, K extends SmartPYControllerName = SmartPYControllerName, F extends SmartPYControllerPropName<K> = SmartPYControllerPropName<...>>(data: {
controller_id: K;
func: F;
args?: any[];
kwargs?: { [key in string]: any; };
timeout?: number;
}): Promise<T>;
... 5 more ...;
returnReceiver([call_id, state, data]: JSRetData): void;
}
AxonCall } from "axon-gui";
async function function bump(): Promise<number>bump() {
const const n: numbern = await const AxonCall: {
init(): Promise<void>;
__call__<K extends SmartPYControllerName, F extends SmartPYControllerPropName<K>, T = unknown>({ controller_id, prop, args, kwargs, timeout, type, }: {
controller_id: K;
prop: F;
args?: any[];
kwargs?: { [key in string]: any; };
timeout?: number;
type: JSCallType;
}): Promise<T>;
call<T = unknown, K extends SmartPYControllerName = SmartPYControllerName, F extends SmartPYControllerPropName<K> = SmartPYControllerPropName<...>>(data: {
controller_id: K;
func: F;
args?: any[];
kwargs?: { [key in string]: any; };
timeout?: number;
}): Promise<T>;
... 5 more ...;
returnReceiver([call_id, state, data]: JSRetData): void;
}
AxonCall.getVar<number, SmartPYControllerName, string>({ controller_id, var_name, timeout, }: {
controller_id: SmartPYControllerName;
var_name: string;
timeout?: number;
}): Promise<number>
getVar<number>({ controller_id: SmartPYControllerNamecontroller_id: "DemoController", var_name: stringvar_name: "counter" });
return const n: numbern + 1;
}Key points
- The attribute must exist: reading a name that Python does not have fails.
- JSON types only: numbers, strings, booleans,
None, arrays, objects. - Do not do heavy work in attributes:
@propertyruns on a worker thread; slow logic belongs in a method (ideallyasync). - Attributes are for state; when you need parameters or a structured return, a method is clearer.