rx-rust 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rx_rust-0.1.0/Cargo.toml +14 -0
- rx_rust-0.1.0/LICENSE +21 -0
- rx_rust-0.1.0/PKG-INFO +329 -0
- rx_rust-0.1.0/README.md +293 -0
- rx_rust-0.1.0/pyproject.toml +62 -0
- rx_rust-0.1.0/python/rx_rust/__init__.py +1083 -0
- rx_rust-0.1.0/rx-rust/.gitignore +1 -0
- rx_rust-0.1.0/rx-rust/Cargo.toml +26 -0
- rx_rust-0.1.0/rx-rust/LICENSE +21 -0
- rx_rust-0.1.0/rx-rust/README.md +735 -0
- rx_rust-0.1.0/rx-rust/src/lib.rs +25 -0
- rx_rust-0.1.0/rx-rust/src/observable/base.rs +127 -0
- rx_rust-0.1.0/rx-rust/src/observable/mod.rs +5 -0
- rx_rust-0.1.0/rx-rust/src/observable/observable.rs +35 -0
- rx_rust-0.1.0/rx-rust/src/observer/mod.rs +3 -0
- rx_rust-0.1.0/rx-rust/src/observer/observer.rs +67 -0
- rx_rust-0.1.0/rx-rust/src/operators/mod.rs +3399 -0
- rx_rust-0.1.0/rx-rust/src/scheduler/mod.rs +161 -0
- rx_rust-0.1.0/rx-rust/src/subject/mod.rs +319 -0
- rx_rust-0.1.0/rx-rust/src/subscription/disposable.rs +109 -0
- rx_rust-0.1.0/rx-rust/src/subscription/mod.rs +3 -0
- rx_rust-0.1.0/rx-rust/tests/integration/observable/mod.rs +1343 -0
- rx_rust-0.1.0/rx-rust/tests/integration/subscription/mod.rs +76 -0
- rx_rust-0.1.0/rx-rust/tests/lib.rs +4 -0
- rx_rust-0.1.0/rx-rust-py/Cargo.lock +185 -0
- rx_rust-0.1.0/rx-rust-py/Cargo.toml +23 -0
- rx_rust-0.1.0/rx-rust-py/LICENSE +21 -0
- rx_rust-0.1.0/rx-rust-py/README.md +293 -0
- rx_rust-0.1.0/rx-rust-py/python/test_rx_rust.py +265 -0
- rx_rust-0.1.0/rx-rust-py/src/lib.rs +938 -0
rx_rust-0.1.0/Cargo.toml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[workspace]
|
|
2
|
+
members = [
|
|
3
|
+
"rx-rust",
|
|
4
|
+
]
|
|
5
|
+
resolver = "2"
|
|
6
|
+
|
|
7
|
+
[workspace.package]
|
|
8
|
+
edition = "2021"
|
|
9
|
+
authors = ["Rx-Rust Team <dev@rx-rust.io>"]
|
|
10
|
+
description = "A Rust-idiomatic reactive programming library"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
repository = "https://github.com/rx-rust/rx-rust"
|
|
13
|
+
keywords = ["reactive", "rx", "observable", "stream"]
|
|
14
|
+
categories = ["asynchronous", "concurrency", "data-structures"]
|
rx_rust-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 RxPY Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
rx_rust-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rx-rust
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Development Status :: 3 - Alpha
|
|
5
|
+
Classifier: Intended Audience :: Developers
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Rust
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Dist: pytest>=7.0 ; extra == 'dev'
|
|
18
|
+
Requires-Dist: sphinx ; extra == 'doc'
|
|
19
|
+
Requires-Dist: sphinx-rtd-theme ; extra == 'doc'
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Provides-Extra: doc
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Summary: Rx-Rust — Reactive Extensions for Python, powered by Rust (PyO3). A high-performance reactive programming library with Observable/Observer/Subject/Scheduler support.
|
|
24
|
+
Keywords: reactive,rx,rx-rust,reactivex,observable,streams,async,event-driven,rust,pyo3,functional-programming
|
|
25
|
+
Home-Page: https://gitcode.com/VictorTop/Rx-Rust
|
|
26
|
+
Author-email: Victor <victor@rx-rust.dev>
|
|
27
|
+
Maintainer: Rx-Rust Contributors
|
|
28
|
+
Requires-Python: >=3.8
|
|
29
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
30
|
+
Project-URL: Bug Reports, https://gitcode.com/VictorTop/Rx-Rust/issues
|
|
31
|
+
Project-URL: Documentation, https://gitcode.com/VictorTop/Rx-Rust/blob/main/GUIDE.md
|
|
32
|
+
Project-URL: Homepage, https://gitcode.com/VictorTop/Rx-Rust
|
|
33
|
+
Project-URL: Repository, https://gitcode.com/VictorTop/Rx-Rust.git
|
|
34
|
+
Project-URL: Source, https://gitcode.com/VictorTop/Rx-Rust
|
|
35
|
+
|
|
36
|
+
# RxPY
|
|
37
|
+
|
|
38
|
+
> Reactive Extensions for Python — powered by Rust 🦀
|
|
39
|
+
|
|
40
|
+
[](https://www.rust-lang.org/)
|
|
41
|
+
[](https://www.python.org/)
|
|
42
|
+
[](LICENSE)
|
|
43
|
+
[](pyproject.toml)
|
|
44
|
+
|
|
45
|
+
RxPY 是一个用于组合异步和基于事件的程序的 Python 库,灵感来自微软的 Reactive Extensions (Rx)。
|
|
46
|
+
它通过 **PyO3** 绑定高性能 **Rust** 实现,提供零开销的响应式编程体验。
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## ✨ 特性
|
|
51
|
+
|
|
52
|
+
### 核心类型
|
|
53
|
+
- **`Observable`** — 在未来可能发射 0 个或多个值的惰性序列
|
|
54
|
+
- **`Subscription`** — 订阅句柄,支持 `dispose()` 取消订阅
|
|
55
|
+
- **`Observer`** — 订阅者回调(`on_next` / `on_completed`)
|
|
56
|
+
|
|
57
|
+
### Subjects(广播器)
|
|
58
|
+
- **`PublishSubject`** — 广播型主题,向所有订阅者发送值
|
|
59
|
+
- **`BehaviorSubject`** — 带"当前值"的主题,新订阅者会立即收到最新值
|
|
60
|
+
- **`ReplaySubject`** — 重放历史值的主题,缓存最近 N 个值
|
|
61
|
+
|
|
62
|
+
### 调度器
|
|
63
|
+
- **`CurrentThreadScheduler`** — 在当前线程同步执行
|
|
64
|
+
- **`ImmediateScheduler`** — 立即执行,不做调度
|
|
65
|
+
- **`ThreadPoolScheduler`** — 在线程池中并发执行
|
|
66
|
+
- **`AsyncScheduler`** — 异步执行
|
|
67
|
+
|
|
68
|
+
### 操作符(全部方法链式可组合)
|
|
69
|
+
| 类别 | 操作符 |
|
|
70
|
+
|------|--------|
|
|
71
|
+
| **创建** | `of`, `from_iter`, `range`, `repeat`, `empty`, `never` |
|
|
72
|
+
| **转换** | `map`, `flat_map`, `scan`, `reduce` |
|
|
73
|
+
| **过滤** | `filter`, `take`, `skip`, `first`, `take_while`, `skip_while`, `distinct`, `distinct_until_changed`, `element_at`, `default_if_empty`, `ignore_elements` |
|
|
74
|
+
| **组合** | `merge`, `concat`, `zip`, `combine_latest`, `switch_map` |
|
|
75
|
+
| **数学** | `count`, `sum`, `min`, `max`, `average` |
|
|
76
|
+
| **时间** | `debounce`, `throttle`, `timeout` |
|
|
77
|
+
| **错误** | `catch_error`, `on_error_resume_next`, `retry`, `retry_when` |
|
|
78
|
+
| **调度** | `subscribe_on`, `observe_on` |
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## 🚀 快速开始
|
|
83
|
+
|
|
84
|
+
### 安装
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
# 从 PyPI 安装(推荐)
|
|
88
|
+
pip install rxpy
|
|
89
|
+
|
|
90
|
+
# 或者从源码构建(需要 Rust 工具链)
|
|
91
|
+
git clone https://gitcode.com/VictorTop/Rx-Rust.git
|
|
92
|
+
cd Rx-Rust/rxpy
|
|
93
|
+
pip install maturin
|
|
94
|
+
maturin develop
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 三分钟第一个程序
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
import rxpy
|
|
101
|
+
|
|
102
|
+
# 1. 创建: 从 1 到 5 的序列
|
|
103
|
+
observable = rxpy.Observable.range(1, 5)
|
|
104
|
+
|
|
105
|
+
# 2. 管道: 过滤偶数 + 平方
|
|
106
|
+
processed = (
|
|
107
|
+
observable
|
|
108
|
+
.filter(lambda x: x % 2 == 0)
|
|
109
|
+
.map(lambda x: x * x)
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
# 3. 订阅并打印
|
|
113
|
+
sub = processed.subscribe(
|
|
114
|
+
on_next=lambda v: print(f"收到: {v}"),
|
|
115
|
+
on_completed=lambda: print("完成!"),
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# 也可以用上下文管理器自动取消
|
|
119
|
+
# with processed.subscribe(on_next=print) as sub:
|
|
120
|
+
# pass # 退出时自动 sub.dispose()
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**运行结果:**
|
|
124
|
+
```text
|
|
125
|
+
收到: 4
|
|
126
|
+
收到: 16
|
|
127
|
+
完成!
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## 📚 使用指南
|
|
133
|
+
|
|
134
|
+
### 1. Subject 示例:事件总线
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
import rxpy
|
|
138
|
+
|
|
139
|
+
# 创建一个发布主题
|
|
140
|
+
subject = rxpy.PublishSubject()
|
|
141
|
+
|
|
142
|
+
# 订阅者 A
|
|
143
|
+
result_a = []
|
|
144
|
+
subject.subscribe(
|
|
145
|
+
on_next=lambda v: result_a.append(("A", v)),
|
|
146
|
+
on_completed=lambda: result_a.append("A done"),
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# 发射值
|
|
150
|
+
subject.on_next(1) # A 收到 1
|
|
151
|
+
subject.on_next(2) # A 收到 2
|
|
152
|
+
|
|
153
|
+
# 订阅者 B(迟到的订阅者)
|
|
154
|
+
result_b = []
|
|
155
|
+
subject.subscribe(
|
|
156
|
+
on_next=lambda v: result_b.append(("B", v)),
|
|
157
|
+
on_completed=lambda: result_b.append("B done"),
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
subject.on_next(3) # A 和 B 都收到 3
|
|
161
|
+
subject.on_completed() # A 和 B 都收到完成信号
|
|
162
|
+
|
|
163
|
+
print(result_a) # [("A", 1), ("A", 2), ("A", 3), "A done"]
|
|
164
|
+
print(result_b) # [("B", 3), "B done"]
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### 2. BehaviorSubject:带状态的主题
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import rxpy
|
|
171
|
+
|
|
172
|
+
# 初始值为 0
|
|
173
|
+
subject = rxpy.BehaviorSubject(0)
|
|
174
|
+
|
|
175
|
+
# 订阅者 A 立即收到 0
|
|
176
|
+
result_a = []
|
|
177
|
+
subject.subscribe(on_next=lambda v: result_a.append(v))
|
|
178
|
+
|
|
179
|
+
subject.on_next(1) # A 收到 1
|
|
180
|
+
subject.on_next(2) # A 收到 2
|
|
181
|
+
|
|
182
|
+
# 订阅者 B 立即收到当前值 2,以及后续值
|
|
183
|
+
result_b = []
|
|
184
|
+
subject.subscribe(on_next=lambda v: result_b.append(v))
|
|
185
|
+
|
|
186
|
+
subject.on_next(3) # A 和 B 都收到 3
|
|
187
|
+
|
|
188
|
+
print(result_a) # [0, 1, 2, 3]
|
|
189
|
+
print(result_b) # [2, 3]
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 3. ReplaySubject:重放历史
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
import rxpy
|
|
196
|
+
|
|
197
|
+
# 缓存最近 2 个值
|
|
198
|
+
subject = rxpy.ReplaySubject(2)
|
|
199
|
+
|
|
200
|
+
subject.on_next(1)
|
|
201
|
+
subject.on_next(2)
|
|
202
|
+
subject.on_next(3)
|
|
203
|
+
|
|
204
|
+
# 订阅者会收到缓存的 2 和 3
|
|
205
|
+
result = []
|
|
206
|
+
subject.subscribe(on_next=lambda v: result.append(v))
|
|
207
|
+
subject.on_completed()
|
|
208
|
+
|
|
209
|
+
print(result) # [2, 3]
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### 4. 数学与聚合
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
import rxpy
|
|
216
|
+
|
|
217
|
+
# 求和
|
|
218
|
+
result = (
|
|
219
|
+
rxpy.Observable.from_iter([1, 2, 3, 4, 5])
|
|
220
|
+
.reduce(0, lambda acc, x: acc + x)
|
|
221
|
+
.collect()
|
|
222
|
+
)
|
|
223
|
+
print(result) # [15]
|
|
224
|
+
|
|
225
|
+
# 使用 collect() 直接收集所有值到列表
|
|
226
|
+
values = rxpy.Observable.range(1, 5).collect()
|
|
227
|
+
print(values) # [1, 2, 3, 4, 5]
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### 5. 过滤与转换管道
|
|
231
|
+
|
|
232
|
+
```python
|
|
233
|
+
import rxpy
|
|
234
|
+
|
|
235
|
+
result = (
|
|
236
|
+
rxpy.Observable.from_iter(range(1, 11)) # 1..10
|
|
237
|
+
.filter(lambda x: x % 2 == 0) # 只保留偶数: 2, 4, 6, 8, 10
|
|
238
|
+
.map(lambda x: x * 2) # 翻倍: 4, 8, 12, 16, 20
|
|
239
|
+
.take(3) # 只取前 3 个: 4, 8, 12
|
|
240
|
+
.collect()
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
print(result) # [4, 8, 12]
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## 🏗️ 架构
|
|
249
|
+
|
|
250
|
+
```text
|
|
251
|
+
+-------------------+
|
|
252
|
+
| Python API |
|
|
253
|
+
| (rxpy/__init__.py)|
|
|
254
|
+
+---------+---------+
|
|
255
|
+
|
|
|
256
|
+
绑定
|
|
257
|
+
|
|
|
258
|
+
+---------v---------+
|
|
259
|
+
| PyO3 FFI |
|
|
260
|
+
| (Cargo.toml + |
|
|
261
|
+
| src/lib.rs) |
|
|
262
|
+
+---------+---------+
|
|
263
|
+
|
|
|
264
|
+
调用
|
|
265
|
+
|
|
|
266
|
+
+------------------------v------------------------+
|
|
267
|
+
| rx-rust (Rust 核心库) |
|
|
268
|
+
| src/lib.rs + src/observable + src/operators/ |
|
|
269
|
+
| + src/subject + src/scheduler + src/observer |
|
|
270
|
+
+------------------------+------------------------+
|
|
271
|
+
|
|
|
272
|
+
62 个单元测试
|
|
273
|
+
|
|
|
274
|
+
+--------v--------+
|
|
275
|
+
| cargo test ✅ |
|
|
276
|
+
+-----------------+
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
RxPY 构建在 Rust 库 `rx-rust` 之上,所有核心逻辑(Observable、操作符、Subject、调度器)均由 Rust 实现,并通过 PyO3 暴露给 Python。
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## 📖 更多文档
|
|
284
|
+
|
|
285
|
+
- **[GUIDE.md](../GUIDE.md)** — 完整使用指南,从入门到进阶,包含 Rust 和 Python 两个 API 的详细说明
|
|
286
|
+
- **[README.md](../README.md)** — 项目总览
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## 🔧 开发与测试
|
|
291
|
+
|
|
292
|
+
### 环境要求
|
|
293
|
+
- Rust 1.75+
|
|
294
|
+
- Python 3.8+
|
|
295
|
+
- Maturin
|
|
296
|
+
|
|
297
|
+
### 构建
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
cd rxpy
|
|
301
|
+
pip install maturin
|
|
302
|
+
maturin develop # 开发模式安装
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### 测试
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
# Rust 测试(在 rx-rust 目录下)
|
|
309
|
+
cd ../rx-rust
|
|
310
|
+
cargo test
|
|
311
|
+
|
|
312
|
+
# Python 测试(在 rxpy 目录下)
|
|
313
|
+
cd ../rxpy
|
|
314
|
+
python -c "
|
|
315
|
+
import rxpy
|
|
316
|
+
# 基本功能测试
|
|
317
|
+
result = rxpy.Observable.range(1, 5).filter(lambda x: x % 2 == 0).map(lambda x: x * 10).collect()
|
|
318
|
+
print('PASS:', result == [20, 40])
|
|
319
|
+
"
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
## 📄 许可证
|
|
325
|
+
|
|
326
|
+
MIT License © RxPY Contributors
|
|
327
|
+
|
|
328
|
+
请查看 [LICENSE](LICENSE) 文件以获取完整的许可证文本。
|
|
329
|
+
|
rx_rust-0.1.0/README.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# RxPY
|
|
2
|
+
|
|
3
|
+
> Reactive Extensions for Python — powered by Rust 🦀
|
|
4
|
+
|
|
5
|
+
[](https://www.rust-lang.org/)
|
|
6
|
+
[](https://www.python.org/)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
[](pyproject.toml)
|
|
9
|
+
|
|
10
|
+
RxPY 是一个用于组合异步和基于事件的程序的 Python 库,灵感来自微软的 Reactive Extensions (Rx)。
|
|
11
|
+
它通过 **PyO3** 绑定高性能 **Rust** 实现,提供零开销的响应式编程体验。
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ✨ 特性
|
|
16
|
+
|
|
17
|
+
### 核心类型
|
|
18
|
+
- **`Observable`** — 在未来可能发射 0 个或多个值的惰性序列
|
|
19
|
+
- **`Subscription`** — 订阅句柄,支持 `dispose()` 取消订阅
|
|
20
|
+
- **`Observer`** — 订阅者回调(`on_next` / `on_completed`)
|
|
21
|
+
|
|
22
|
+
### Subjects(广播器)
|
|
23
|
+
- **`PublishSubject`** — 广播型主题,向所有订阅者发送值
|
|
24
|
+
- **`BehaviorSubject`** — 带"当前值"的主题,新订阅者会立即收到最新值
|
|
25
|
+
- **`ReplaySubject`** — 重放历史值的主题,缓存最近 N 个值
|
|
26
|
+
|
|
27
|
+
### 调度器
|
|
28
|
+
- **`CurrentThreadScheduler`** — 在当前线程同步执行
|
|
29
|
+
- **`ImmediateScheduler`** — 立即执行,不做调度
|
|
30
|
+
- **`ThreadPoolScheduler`** — 在线程池中并发执行
|
|
31
|
+
- **`AsyncScheduler`** — 异步执行
|
|
32
|
+
|
|
33
|
+
### 操作符(全部方法链式可组合)
|
|
34
|
+
| 类别 | 操作符 |
|
|
35
|
+
|------|--------|
|
|
36
|
+
| **创建** | `of`, `from_iter`, `range`, `repeat`, `empty`, `never` |
|
|
37
|
+
| **转换** | `map`, `flat_map`, `scan`, `reduce` |
|
|
38
|
+
| **过滤** | `filter`, `take`, `skip`, `first`, `take_while`, `skip_while`, `distinct`, `distinct_until_changed`, `element_at`, `default_if_empty`, `ignore_elements` |
|
|
39
|
+
| **组合** | `merge`, `concat`, `zip`, `combine_latest`, `switch_map` |
|
|
40
|
+
| **数学** | `count`, `sum`, `min`, `max`, `average` |
|
|
41
|
+
| **时间** | `debounce`, `throttle`, `timeout` |
|
|
42
|
+
| **错误** | `catch_error`, `on_error_resume_next`, `retry`, `retry_when` |
|
|
43
|
+
| **调度** | `subscribe_on`, `observe_on` |
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 🚀 快速开始
|
|
48
|
+
|
|
49
|
+
### 安装
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# 从 PyPI 安装(推荐)
|
|
53
|
+
pip install rxpy
|
|
54
|
+
|
|
55
|
+
# 或者从源码构建(需要 Rust 工具链)
|
|
56
|
+
git clone https://gitcode.com/VictorTop/Rx-Rust.git
|
|
57
|
+
cd Rx-Rust/rxpy
|
|
58
|
+
pip install maturin
|
|
59
|
+
maturin develop
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 三分钟第一个程序
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
import rxpy
|
|
66
|
+
|
|
67
|
+
# 1. 创建: 从 1 到 5 的序列
|
|
68
|
+
observable = rxpy.Observable.range(1, 5)
|
|
69
|
+
|
|
70
|
+
# 2. 管道: 过滤偶数 + 平方
|
|
71
|
+
processed = (
|
|
72
|
+
observable
|
|
73
|
+
.filter(lambda x: x % 2 == 0)
|
|
74
|
+
.map(lambda x: x * x)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# 3. 订阅并打印
|
|
78
|
+
sub = processed.subscribe(
|
|
79
|
+
on_next=lambda v: print(f"收到: {v}"),
|
|
80
|
+
on_completed=lambda: print("完成!"),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
# 也可以用上下文管理器自动取消
|
|
84
|
+
# with processed.subscribe(on_next=print) as sub:
|
|
85
|
+
# pass # 退出时自动 sub.dispose()
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**运行结果:**
|
|
89
|
+
```text
|
|
90
|
+
收到: 4
|
|
91
|
+
收到: 16
|
|
92
|
+
完成!
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## 📚 使用指南
|
|
98
|
+
|
|
99
|
+
### 1. Subject 示例:事件总线
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
import rxpy
|
|
103
|
+
|
|
104
|
+
# 创建一个发布主题
|
|
105
|
+
subject = rxpy.PublishSubject()
|
|
106
|
+
|
|
107
|
+
# 订阅者 A
|
|
108
|
+
result_a = []
|
|
109
|
+
subject.subscribe(
|
|
110
|
+
on_next=lambda v: result_a.append(("A", v)),
|
|
111
|
+
on_completed=lambda: result_a.append("A done"),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
# 发射值
|
|
115
|
+
subject.on_next(1) # A 收到 1
|
|
116
|
+
subject.on_next(2) # A 收到 2
|
|
117
|
+
|
|
118
|
+
# 订阅者 B(迟到的订阅者)
|
|
119
|
+
result_b = []
|
|
120
|
+
subject.subscribe(
|
|
121
|
+
on_next=lambda v: result_b.append(("B", v)),
|
|
122
|
+
on_completed=lambda: result_b.append("B done"),
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
subject.on_next(3) # A 和 B 都收到 3
|
|
126
|
+
subject.on_completed() # A 和 B 都收到完成信号
|
|
127
|
+
|
|
128
|
+
print(result_a) # [("A", 1), ("A", 2), ("A", 3), "A done"]
|
|
129
|
+
print(result_b) # [("B", 3), "B done"]
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 2. BehaviorSubject:带状态的主题
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
import rxpy
|
|
136
|
+
|
|
137
|
+
# 初始值为 0
|
|
138
|
+
subject = rxpy.BehaviorSubject(0)
|
|
139
|
+
|
|
140
|
+
# 订阅者 A 立即收到 0
|
|
141
|
+
result_a = []
|
|
142
|
+
subject.subscribe(on_next=lambda v: result_a.append(v))
|
|
143
|
+
|
|
144
|
+
subject.on_next(1) # A 收到 1
|
|
145
|
+
subject.on_next(2) # A 收到 2
|
|
146
|
+
|
|
147
|
+
# 订阅者 B 立即收到当前值 2,以及后续值
|
|
148
|
+
result_b = []
|
|
149
|
+
subject.subscribe(on_next=lambda v: result_b.append(v))
|
|
150
|
+
|
|
151
|
+
subject.on_next(3) # A 和 B 都收到 3
|
|
152
|
+
|
|
153
|
+
print(result_a) # [0, 1, 2, 3]
|
|
154
|
+
print(result_b) # [2, 3]
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### 3. ReplaySubject:重放历史
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
import rxpy
|
|
161
|
+
|
|
162
|
+
# 缓存最近 2 个值
|
|
163
|
+
subject = rxpy.ReplaySubject(2)
|
|
164
|
+
|
|
165
|
+
subject.on_next(1)
|
|
166
|
+
subject.on_next(2)
|
|
167
|
+
subject.on_next(3)
|
|
168
|
+
|
|
169
|
+
# 订阅者会收到缓存的 2 和 3
|
|
170
|
+
result = []
|
|
171
|
+
subject.subscribe(on_next=lambda v: result.append(v))
|
|
172
|
+
subject.on_completed()
|
|
173
|
+
|
|
174
|
+
print(result) # [2, 3]
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 4. 数学与聚合
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
import rxpy
|
|
181
|
+
|
|
182
|
+
# 求和
|
|
183
|
+
result = (
|
|
184
|
+
rxpy.Observable.from_iter([1, 2, 3, 4, 5])
|
|
185
|
+
.reduce(0, lambda acc, x: acc + x)
|
|
186
|
+
.collect()
|
|
187
|
+
)
|
|
188
|
+
print(result) # [15]
|
|
189
|
+
|
|
190
|
+
# 使用 collect() 直接收集所有值到列表
|
|
191
|
+
values = rxpy.Observable.range(1, 5).collect()
|
|
192
|
+
print(values) # [1, 2, 3, 4, 5]
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### 5. 过滤与转换管道
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
import rxpy
|
|
199
|
+
|
|
200
|
+
result = (
|
|
201
|
+
rxpy.Observable.from_iter(range(1, 11)) # 1..10
|
|
202
|
+
.filter(lambda x: x % 2 == 0) # 只保留偶数: 2, 4, 6, 8, 10
|
|
203
|
+
.map(lambda x: x * 2) # 翻倍: 4, 8, 12, 16, 20
|
|
204
|
+
.take(3) # 只取前 3 个: 4, 8, 12
|
|
205
|
+
.collect()
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
print(result) # [4, 8, 12]
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## 🏗️ 架构
|
|
214
|
+
|
|
215
|
+
```text
|
|
216
|
+
+-------------------+
|
|
217
|
+
| Python API |
|
|
218
|
+
| (rxpy/__init__.py)|
|
|
219
|
+
+---------+---------+
|
|
220
|
+
|
|
|
221
|
+
绑定
|
|
222
|
+
|
|
|
223
|
+
+---------v---------+
|
|
224
|
+
| PyO3 FFI |
|
|
225
|
+
| (Cargo.toml + |
|
|
226
|
+
| src/lib.rs) |
|
|
227
|
+
+---------+---------+
|
|
228
|
+
|
|
|
229
|
+
调用
|
|
230
|
+
|
|
|
231
|
+
+------------------------v------------------------+
|
|
232
|
+
| rx-rust (Rust 核心库) |
|
|
233
|
+
| src/lib.rs + src/observable + src/operators/ |
|
|
234
|
+
| + src/subject + src/scheduler + src/observer |
|
|
235
|
+
+------------------------+------------------------+
|
|
236
|
+
|
|
|
237
|
+
62 个单元测试
|
|
238
|
+
|
|
|
239
|
+
+--------v--------+
|
|
240
|
+
| cargo test ✅ |
|
|
241
|
+
+-----------------+
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
RxPY 构建在 Rust 库 `rx-rust` 之上,所有核心逻辑(Observable、操作符、Subject、调度器)均由 Rust 实现,并通过 PyO3 暴露给 Python。
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## 📖 更多文档
|
|
249
|
+
|
|
250
|
+
- **[GUIDE.md](../GUIDE.md)** — 完整使用指南,从入门到进阶,包含 Rust 和 Python 两个 API 的详细说明
|
|
251
|
+
- **[README.md](../README.md)** — 项目总览
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## 🔧 开发与测试
|
|
256
|
+
|
|
257
|
+
### 环境要求
|
|
258
|
+
- Rust 1.75+
|
|
259
|
+
- Python 3.8+
|
|
260
|
+
- Maturin
|
|
261
|
+
|
|
262
|
+
### 构建
|
|
263
|
+
|
|
264
|
+
```bash
|
|
265
|
+
cd rxpy
|
|
266
|
+
pip install maturin
|
|
267
|
+
maturin develop # 开发模式安装
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### 测试
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
# Rust 测试(在 rx-rust 目录下)
|
|
274
|
+
cd ../rx-rust
|
|
275
|
+
cargo test
|
|
276
|
+
|
|
277
|
+
# Python 测试(在 rxpy 目录下)
|
|
278
|
+
cd ../rxpy
|
|
279
|
+
python -c "
|
|
280
|
+
import rxpy
|
|
281
|
+
# 基本功能测试
|
|
282
|
+
result = rxpy.Observable.range(1, 5).filter(lambda x: x % 2 == 0).map(lambda x: x * 10).collect()
|
|
283
|
+
print('PASS:', result == [20, 40])
|
|
284
|
+
"
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
## 📄 许可证
|
|
290
|
+
|
|
291
|
+
MIT License © RxPY Contributors
|
|
292
|
+
|
|
293
|
+
请查看 [LICENSE](LICENSE) 文件以获取完整的许可证文本。
|