| 1 |
--- |
| 2 |
title: "nanonext - Messaging and Async I/O" |
| 3 |
vignette: > |
| 4 |
%\VignetteIndexEntry{nanonext - Messaging and Async I/O} |
| 5 |
%\VignetteEngine{litedown::vignette} |
| 6 |
%\VignetteEncoding{UTF-8} |
| 7 |
--- |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
### 1. Cross-language Exchange |
| 12 |
|
| 13 |
`nanonext` provides a fast, reliable data interface between different programming languages where NNG has an implementation, including C, C++, Java, Python, Go, and Rust. |
| 14 |
|
| 15 |
This messaging interface is lightweight, robust, and has limited points of failure. It enables: |
| 16 |
|
| 17 |
- Communication between processes in the same or different languages |
| 18 |
- Distributed computing across networks or on the same machine |
| 19 |
- Real-time data pipelines where computation times exceed data frequency |
| 20 |
- Modular software design following Unix philosophy |
| 21 |
|
| 22 |
This example demonstrates numerical data exchange between R and Python (NumPy). |
| 23 |
|
| 24 |
Create socket in Python using the NNG binding 'pynng': |
| 25 |
|
| 26 |
|
| 27 |
``` python |
| 28 |
import numpy as np |
| 29 |
import pynng |
| 30 |
socket = pynng.Pair0(listen="ipc:///tmp/nanonext.socket") |
| 31 |
``` |
| 32 |
|
| 33 |
Create nano object in R using `nanonext`, then send a vector of 'doubles', specifying mode as 'raw': |
| 34 |
|
| 35 |
|
| 36 |
``` r |
| 37 |
library(nanonext) |
| 38 |
n <- nano("pair", dial = "ipc:///tmp/nanonext.socket") |
| 39 |
n$send(c(1.1, 2.2, 3.3, 4.4, 5.5), mode = "raw") |
| 40 |
#> [1] 0 |
| 41 |
``` |
| 42 |
|
| 43 |
Receive in Python as a NumPy array of 'floats', and send back to R: |
| 44 |
|
| 45 |
|
| 46 |
``` python |
| 47 |
raw = socket.recv() |
| 48 |
array = np.frombuffer(raw) |
| 49 |
print(array) |
| 50 |
#> [1.1 2.2 3.3 4.4 5.5] |
| 51 |
|
| 52 |
msg = array.tobytes() |
| 53 |
socket.send(msg) |
| 54 |
|
| 55 |
socket.close() |
| 56 |
``` |
| 57 |
|
| 58 |
Receive in R, specifying the receive mode as 'double': |
| 59 |
|
| 60 |
|
| 61 |
``` r |
| 62 |
n$recv(mode = "double") |
| 63 |
#> [1] 1.1 2.2 3.3 4.4 5.5 |
| 64 |
|
| 65 |
n$close() |
| 66 |
``` |
| 67 |
|
| 68 |
### 2. Async and Concurrency |
| 69 |
|
| 70 |
`nanonext` implements true async send and receive, leveraging NNG as a massively-scalable concurrency framework. |
| 71 |
|
| 72 |
|
| 73 |
``` r |
| 74 |
s1 <- socket("pair", listen = "inproc://nano") |
| 75 |
s2 <- socket("pair", dial = "inproc://nano") |
| 76 |
|
| 77 |
``` |
| 78 |
|
| 79 |
`send_aio()` and `recv_aio()` return immediately with an 'Aio' object that performs operations asynchronously. Aio objects return an unresolved value while the operation is ongoing, then automatically resolve once complete. |
| 80 |
|
| 81 |
|
| 82 |
``` r |
| 83 |
# async receive requested, but no messages waiting yet |
| 84 |
msg <- recv_aio(s2) |
| 85 |
msg |
| 86 |
#> < recvAio | $data > |
| 87 |
msg$data |
| 88 |
#> 'unresolved' logi NA |
| 89 |
``` |
| 90 |
|
| 91 |
For 'sendAio' objects, the result is stored at `$result`. For 'recvAio' objects, the message is stored at `$data`. |
| 92 |
|
| 93 |
|
| 94 |
``` r |
| 95 |
res <- send_aio(s1, data.frame(a = 1, b = 2)) |
| 96 |
res |
| 97 |
#> < sendAio | $result > |
| 98 |
res$result |
| 99 |
#> [1] 0 |
| 100 |
``` |
| 101 |
> 0 indicates successful send - the message has been accepted by the socket for sending but may still be buffered within the system. |
| 102 |
|
| 103 |
|
| 104 |
``` r |
| 105 |
# once a message is sent, the recvAio resolves automatically |
| 106 |
msg$data |
| 107 |
#> a b |
| 108 |
#> 1 1 2 |
| 109 |
``` |
| 110 |
|
| 111 |
Use `unresolved()` in control flow to perform actions before or after Aio resolution without blocking. |
| 112 |
|
| 113 |
|
| 114 |
``` r |
| 115 |
msg <- recv_aio(s2) |
| 116 |
|
| 117 |
# unresolved() checks resolution status |
| 118 |
while (unresolved(msg)) { |
| 119 |
# perform other tasks while waiting |
| 120 |
send_aio(s1, "resolved") |
| 121 |
cat("unresolved") |
| 122 |
} |
| 123 |
#> unresolved |
| 124 |
|
| 125 |
# access resolved value |
| 126 |
msg$data |
| 127 |
#> [1] "resolved" |
| 128 |
``` |
| 129 |
|
| 130 |
Explicitly wait for completion with `call_aio()` (blocking). |
| 131 |
|
| 132 |
|
| 133 |
``` r |
| 134 |
# wait for completion and return resolved Aio |
| 135 |
call_aio(msg) |
| 136 |
|
| 137 |
# access resolved value (waiting if required): |
| 138 |
call_aio(msg)$data |
| 139 |
#> [1] "resolved" |
| 140 |
|
| 141 |
# or directly: |
| 142 |
collect_aio(msg) |
| 143 |
#> [1] "resolved" |
| 144 |
|
| 145 |
# or user-interruptible: |
| 146 |
msg[] |
| 147 |
#> [1] "resolved" |
| 148 |
|
| 149 |
close(s1) |
| 150 |
close(s2) |
| 151 |
|
| 152 |
``` |
| 153 |
|
| 154 |
### 3. Synchronisation Primitives |
| 155 |
|
| 156 |
`nanonext` implements cross-platform synchronisation primitives from the NNG library, enabling synchronisation between NNG events and the main R execution thread. |
| 157 |
|
| 158 |
Condition variables can signal events such as asynchronous receive completions and pipe events (connections established or dropped). Each condition variable has a value (counter) and flag (boolean). Signals increment the value; successful `wait()` or `until()` calls decrement it. A non-zero value allows waiting threads to continue. |
| 159 |
|
| 160 |
This approach is more efficient than polling - consuming no resources while waiting and synchronising with zero latency. |
| 161 |
|
| 162 |
**Example 1: Wait for connection** |
| 163 |
|
| 164 |
|
| 165 |
``` r |
| 166 |
sock <- socket("pair", listen = "inproc://nanopipe") |
| 167 |
|
| 168 |
cv <- cv() |
| 169 |
cv_value(cv) |
| 170 |
#> [1] 0 |
| 171 |
|
| 172 |
pipe_notify(sock, cv = cv, add = TRUE, remove = TRUE) |
| 173 |
|
| 174 |
# wait(cv) would block until connection established |
| 175 |
|
| 176 |
# for illustration: |
| 177 |
sock2 <- socket("pair", dial = "inproc://nanopipe") |
| 178 |
|
| 179 |
cv_value(cv) # incremented when pipe created |
| 180 |
#> [1] 1 |
| 181 |
|
| 182 |
wait(cv) # does not block as cv value is non-zero |
| 183 |
|
| 184 |
cv_value(cv) # decremented by wait() |
| 185 |
#> [1] 0 |
| 186 |
|
| 187 |
close(sock2) |
| 188 |
|
| 189 |
cv_value(cv) # incremented when pipe destroyed |
| 190 |
#> [1] 1 |
| 191 |
|
| 192 |
close(sock) |
| 193 |
|
| 194 |
``` |
| 195 |
|
| 196 |
**Example 2: Wait for message or disconnection** |
| 197 |
|
| 198 |
|
| 199 |
``` r |
| 200 |
sock <- socket("pair", listen = "inproc://nanosignal") |
| 201 |
sock2 <- socket("pair", dial = "inproc://nanosignal") |
| 202 |
|
| 203 |
cv <- cv() |
| 204 |
cv_value(cv) |
| 205 |
#> [1] 0 |
| 206 |
|
| 207 |
pipe_notify(sock, cv = cv, add = FALSE, remove = TRUE, flag = TRUE) |
| 208 |
|
| 209 |
send(sock2, "this message will wake waiting thread") |
| 210 |
#> [1] 0 |
| 211 |
|
| 212 |
r <- recv_aio(sock, cv = cv) |
| 213 |
|
| 214 |
# wakes when async receive completes |
| 215 |
wait(cv) || stop("peer disconnected") |
| 216 |
#> [1] TRUE |
| 217 |
|
| 218 |
r$data |
| 219 |
#> [1] "this message will wake waiting thread" |
| 220 |
|
| 221 |
close(sock) |
| 222 |
close(sock2) |
| 223 |
|
| 224 |
``` |
| 225 |
|
| 226 |
When `flag = TRUE` is set for pipe notifications, `wait()` returns FALSE for pipe events (rather than TRUE for message events). This distinguishes between disconnections and successful receives, something not possible using `call_aio()` alone. |
| 227 |
|
| 228 |
This mechanism enables waiting simultaneously on multiple events while distinguishing between them. `pipe_notify()` can signal up to two condition variables per event for additional flexibility in concurrent applications. |