-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.qmd
More file actions
174 lines (127 loc) · 2.89 KB
/
python.qmd
File metadata and controls
174 lines (127 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
title: "Python"
---
## Getting Started
Install [`rembus`](https://pypi.org/project/rembus/) using `pip` or `uv`:
```bash
pip install rembus
```
```bash
uv add rembus
```
For the basic concepts about node types and addressing see the
[Julia docs](julia.qmd#rembus-node-types)
Rembus provides both synchronous and asynchronous APIs:
```python
import rembus as rb
# Synchronous API
cli = rb.node("mynode")
```
```python
import rembus as rb
# Asynchronous API
cli = await rembus.component("mynode")
```
The returned `cli` object represents a Rembus component and exposes the core
communication primitives:
- `rpc` — request a remote service
- `expose` — expose a local function as an RPC service
- `publish` — publish messages to a topic
- `subscribe` — subscribe to topic updates
---
## RPC Call
```{mermaid}
flowchart LR
C(("Client")) --> S((Server))
style C fill:#009E73, color:white
style S fill:#0072B2, color:white
```
This example shows a Python client invoking an RPC service implemented in Julia.
### Julia Server
```julia
using Statistics
using Rembus
function stats(df)
return Dict(
"min" => minimum(df.value),
"max" => maximum(df.value),
"mean" => mean(df.value),
"std" => std(df.value)
)
end
bro = component()
expose(bro, stats)
println("up and running")
wait(bro)
```
### Python Client
```python
import rembus as rb
import pandas as pd
from random import random
nrows = 1_000_000
df = pd.DataFrame({
"name": [f"kpi_{i}" for i in range(nrows)],
"ts": range(nrows),
"value": [random() for _ in range(nrows)],
})
cli = rembus.node("foo")
summary = cli.rpc("stats", df)
cli.close()
```
### Data Flow
1. The Python client sends a pandas DataFrame.
2. Rembus converts it into a Julia DataFrame.
3. The Julia service executes the computation.
4. The result is returned as a Python dictionary.
5. The WS connection is closed.
---
## Publish / Subscribe
This example shows a couple of subsribed nodes implemented in Python and a
Julia publisher.
```{mermaid}
flowchart LR
C(("Publisher")) --> S1((Subscriber 1))
C --> S2((Subscriber 2))
style C fill:#009E73, color:white
style S1 fill:#0072B2, color:white
style S2 fill:#0072B2, color:white
```
### Subscribers (Python)
```python
import rembus as rb
def mytopic(data):
print(f"[Sub-1] mytopic: {data}")
sub = rembus.node(name="sub-1")
sub.subscribe(mytopic)
print("up and running")
sub.wait()
```
```python
import rembus
def mytopic(data):
print(f"[Sub-2] mytopic: {data}")
sub = rembus.node(name="sub-2")
sub.subscribe(mytopic)
print("up and running")
sub.wait()
```
### Publisher (Julia)
```julia
using Rembus
pub = component([
"ws://:3001/client",
"ws://:3002/client"
])
sensor1 = Dict("T" => 18.3, "H" => 45.2)
sensor2 = Dict("P" => 2.3)
publish(
pub,
"mytopic",
Dict(
"sensor#1" => sensor1,
"sensor#2" => sensor2,
)
)
close(pub)
```