You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An 'Aio' object returns an unresolved value whilst its asynchronous operation is ongoing, automatically resolving to a final value once complete.
92
92
93
93
94
-
```r
94
+
```r
95
95
# an async receive is requested, but no messages are waiting (yet to be sent)
96
96
msg<- recv_aio(s2)
97
97
msg
@@ -103,7 +103,7 @@ msg$data
103
103
For a 'sendAio' object, the result is stored at `$result`.
104
104
105
105
106
-
```r
106
+
```r
107
107
res<- send_aio(s1, data.frame(a=1, b=2))
108
108
res
109
109
#> < sendAio | $result >
@@ -115,7 +115,7 @@ res$result
115
115
For a 'recvAio' object, the message is stored at `$data`.
116
116
117
117
118
-
```r
118
+
```r
119
119
# now that a message has been sent, the 'recvAio' resolves automatically
120
120
msg$data
121
121
#> a b
@@ -125,7 +125,7 @@ msg$data
125
125
Auxiliary function `unresolved()` may be used in control flow statements to perform actions which depend on resolution of the Aio, both before and after. This means there is no need to actually wait (block) for an Aio to resolve, as the example below demonstrates.
126
126
127
127
128
-
```r
128
+
```r
129
129
msg<- recv_aio(s2)
130
130
131
131
# unresolved() queries for resolution itself so no need to use it again within the while loop
@@ -144,14 +144,18 @@ msg$data
144
144
The values may also be called explicitly using `call_aio()`. This will wait for completion of the Aio (blocking).
145
145
146
146
147
-
```r
147
+
```r
148
148
# will wait for completion then return the resolved Aio
149
149
call_aio(msg)
150
150
151
-
# to access the resolved value directly (waiting if required)
151
+
# to access the resolved value (waiting if required):
152
152
call_aio(msg)$data
153
153
#> [1] "resolved"
154
154
155
+
# or directly:
156
+
collect_aio(msg)
157
+
#> [1] "resolved"
158
+
155
159
close(s1)
156
160
close(s2)
157
161
```
@@ -167,7 +171,7 @@ Can be used to perform computationally-expensive calculations or I/O-bound opera
167
171
[S] Server process: `reply()` will wait for a message and apply a function, in this case `rnorm()`, before sending back the result. This is started in a background 'mirai' process.
At this point, the client can run additional code concurrent with the server processing the request.
188
192
189
193
190
-
```r
194
+
```r
191
195
# do more...
192
196
```
193
197
@@ -196,13 +200,9 @@ When the result of the server calculation is required, the `recvAio` may be call
196
200
The return value from the server request is then retrieved and stored in the Aio as `$data`.
197
201
198
202
199
-
```r
200
-
call_aio(aio)
201
-
202
-
aio
203
-
#> < recvAio | $data >
204
-
aio$data|> str()
205
-
#> num [1:100000000] 1.365 -0.842 -0.816 1.367 -0.813 ...
203
+
```r
204
+
call_aio(aio)$data|> str()
205
+
#> num [1:100000000] 0.257 -0.413 0.946 0.545 0.071 ...
206
206
```
207
207
208
208
As `call_aio()` is blocking and will wait for completion, an alternative is to query `aio$data` directly. This will return an 'unresolved' logical NA value if the calculation is yet to complete.
@@ -232,7 +232,7 @@ The following shows how condition variables and signalling work in practice.
232
232
Example 1: set up a socket, and wait for the other side to connect:
233
233
234
234
235
-
```r
235
+
```r
236
236
sock<- socket("pair", listen="inproc://nanopipe")
237
237
238
238
cv<- cv() # create new condition variable
@@ -265,7 +265,7 @@ close(sock)
265
265
Example 2: wait until a message is received or connection is dropped:
@@ -317,11 +317,11 @@ A client configuration requires a PEM-encoded CA certificate (chain) used to ver
317
317
Additionally, the convenience function `write_cert()` can automatically generate a 4096 bit RSA key pair and self-signed X.509 certificate in the format required by `tls_config()`. The 'cn' argument must be provided and match exactly the hostname / IP address of the URL that is being used, e.g. in the example below '127.0.0.1' must be used throughout, or alternatively 'localhost', but not a mixture of the two.
`nanonext` fully implements NNG's pub/sub protocol as per the below example. A subscriber can subscribe to one or multiple topics broadcast by a publisher.
@@ -392,7 +392,7 @@ sub |> recv(mode = "character")
392
392
The subscribed topic can be of any atomic type (not just character), allowing integer, double, logical, complex and raw vectors to be sent and received.
393
393
394
394
395
-
```r
395
+
```r
396
396
sub|> subscribe(topic=1)
397
397
pub|> send(c(1, 10, 10, 20), mode="raw")
398
398
#> [1] 0
@@ -416,7 +416,7 @@ This type of pattern is useful for applications such as service discovery.
416
416
A surveyor sends a survey, which is broadcast to all peer respondents. Respondents are then able to reply, but are not obliged to. The survey itself is a timed event, and responses received after the timeout are discarded.
In this respect, it may be used as a performant and lightweight method for making REST API requests.
@@ -509,7 +509,7 @@ In this respect, it may be used as a performant and lightweight method for makin
509
509
By specifying `convert = FALSE`, the received binary data is made available as a raw vector. This may be fed into 'json' parsers which can operate directly on such data etc.
The stream interface can be used to communicate with (secure) websocket servers. The argument `textframes = TRUE` can be specified where the websocket server uses text rather than binary frames.
`send()` and `recv()`, as well as their asynchronous counterparts `send_aio()` and `recv_aio()` can be used on Streams in the same way as Sockets. This affords a great deal of flexibility in ingesting and processing streaming data.
575
575
576
576
577
-
```r
577
+
```r
578
578
s|> recv()
579
579
#> [1] "echo.websocket.events sponsored by Lob.com"
580
580
@@ -612,7 +612,7 @@ See the function documentation page for a list of common options.
612
612
Once a dialer or listener has started, it is not generally possible to change its configuration. In this case, the dialer or listener should be created specifying 'autostart = FALSE'.
Copy file name to clipboardExpand all lines: vignettes/nanonext.Rmd.orig
+5-6Lines changed: 5 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -137,9 +137,12 @@ The values may also be called explicitly using `call_aio()`. This will wait for
137
137
# will wait for completion then return the resolved Aio
138
138
call_aio(msg)
139
139
140
-
# to access the resolved value directly (waiting if required)
140
+
# to access the resolved value (waiting if required):
141
141
call_aio(msg)$data
142
142
143
+
# or directly:
144
+
collect_aio(msg)
145
+
143
146
close(s1)
144
147
close(s2)
145
148
@@ -184,11 +187,7 @@ When the result of the server calculation is required, the `recvAio` may be call
184
187
The return value from the server request is then retrieved and stored in the Aio as `$data`.
185
188
186
189
```{r rpcclient3}
187
-
call_aio(aio)
188
-
189
-
aio
190
-
aio$data |> str()
191
-
190
+
call_aio(aio)$data |> str()
192
191
```
193
192
194
193
As `call_aio()` is blocking and will wait for completion, an alternative is to query `aio$data` directly. This will return an 'unresolved' logical NA value if the calculation is yet to complete.
0 commit comments