View source: R/trajectory-activities.R
send | R Documentation |
These activities enable asynchronous programming. send()
broadcasts a
signal or a list of signals. Arrivals can subscribe to signals and (optionally)
assign a handler with trap()
. Note that, while inside a batch, all the
signals subscribed before entering the batch are ignored. Upon a signal
reception, the arrival stops the current activity and executes the handler
(if provided). Then, the execution returns to the activity following the
point of the interruption. untrap()
can be used to unsubscribe from
signals. wait()
blocks until a signal is received.
send(.trj, signals, delay = 0, ..., tag)
trap(.trj, signals, handler = NULL, interruptible = TRUE, ..., tag)
untrap(.trj, signals, ..., tag)
wait(.trj, ..., tag)
.trj |
the trajectory object. |
signals |
signal or list of signals, accepts either a string, a list of strings or a callable object (a function) which must return a string or a list of strings. |
delay |
optional timeout to trigger the signals, accepts either a numeric or a callable object (a function) which must return a numeric. |
... |
unused. |
tag |
activity tag name to perform named rollbacks (see
|
handler |
optional trajectory object to handle a signal received. |
interruptible |
whether the handler can be interrupted by signals. |
Returns the trajectory object.
renege_if
## block, signal and continue with a handler
signal <- "you shall pass"
t_blocked <- trajectory() %>%
trap(
signal,
trajectory() %>%
log_("executing the handler")) %>%
log_("waiting...") %>%
wait() %>%
log_("continuing!")
t_signaler <- trajectory() %>%
log_(signal) %>%
send(signal)
simmer() %>%
add_generator("blocked", t_blocked, at(0)) %>%
add_generator("signaler", t_signaler, at(5)) %>%
run() %>% invisible
## handlers can be interrupted, unless interruptible=FALSE
t_worker <- trajectory() %>%
trap(
signal,
handler = trajectory() %>%
log_("ok, I'm packing...") %>%
timeout(1)) %>%
log_("performing a looong task...") %>%
timeout(100) %>%
log_("and I'm leaving!")
simmer() %>%
add_generator("worker", t_worker, at(0)) %>%
add_generator("signaler", t_signaler, at(5, 5.5)) %>%
run() %>% invisible
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.