send: Inter-arrival Communication

View source: R/trajectory-activities.R

sendR Documentation

Inter-arrival Communication

Description

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.

Usage

send(.trj, signals, delay = 0, ..., tag)

trap(.trj, signals, handler = NULL, interruptible = TRUE, ..., tag)

untrap(.trj, signals, ..., tag)

wait(.trj, ..., tag)

Arguments

.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 rollback) or just to better identify your activities.

handler

optional trajectory object to handle a signal received.

interruptible

whether the handler can be interrupted by signals.

Value

Returns the trajectory object.

See Also

renege_if

Examples

## 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


Bart6114/simmer documentation built on Oct. 2, 2024, 4:52 p.m.