Set or view the number of daemons (background processes). Create persistent background processes to receive mirai requests. This provides a potentially more efficient solution for async operations as new processes no longer need to be created on an ad hoc basis.

daemons(...)

Arguments

...

either an integer to set the number of daemons, or 'view' to view the number of currently active daemons.

Value

Depending on the specified ... parameter:

  • integer: integer change in number of daemons (created or destroyed).

  • 'view': integer number of currently set daemons.

  • missing: the 'nanoSocket' for connecting to the daemons, or NULL if it is yet to be created.

Details

{mirai} will revert to the default behaviour of creating a new background process for each request if the number of daemons is set to 0.

The current implementation is low-level and ensures tasks are evenly-distributed amongst daemons without actively managing a task queue. This approach provides a robust and resource-light solution, particularly well-suited to working with similar-length tasks, or where the number of concurrent tasks typically does not exceed the number of available daemons.

Examples

if (interactive()) {
# Only run examples in interactive R sessions

# Create 4 daemons
daemons(4)
# View the number of active daemons
daemons("view")
# Reset to zero
daemons(0)

}