-
Notifications
You must be signed in to change notification settings - Fork 40
Open
Labels
reprexneeds a minimal reproducible exampleneeds a minimal reproducible example
Description
Dear Gábor,
I'm using your beautiful callr package and I'm having trouble because I can't figure out how to execute a background process invoked by a parent process that must terminate its execution immediately after invoking the next one.
In other words, if the parent process ‘dies’ before the child process has finished, the child process is also terminated prematurely.
The only way I have found to make this work is to put a Sys.sleep() into the parent after the callr::r_bg or callr::r_session$call to maintain alive it the time needed by the child process.
(I'm testing it on CentOS7 and R 4.1.2)
Thanks in advance for your help!
Diego
I report 2 functions I wrote invoked by the parent process
# First attempt
runAsync = function(fun_name=NULL, fun_args=list(), src_file=NULL, stdout='|', stderr='|', cmdargs=c('--slave', '--no-save', '--no-restore')) {
wrapper_func = function(src_file, fun_name, fun_args) {
source(src_file)
do.call(get(fun_name), fun_args)
}
callr::r_bg(
func = wrapper_func,
args = list(
src_file = src_file,
fun_name = fun_name,
fun_args = fun_args
),
supervise = FALSE,
stdout = stdout,
stderr = stderr,
cmdargs = cmdargs
)
}
runAsync(
fun_name='my_bg_func',
fun_args=list(par='value1'),
src_file='~/functions_source.R')
#Sys.sleep(35) # only with this does it work
-------------------------------------------------------
# Second attempt
runProcess = function(fun_name=NULL, fun_args=list(), src_file=NULL) {
options = callr::r_session_options()
options$supervise = FALSE
rs = callr::r_session$new(wait=TRUE, options=options)
rs$call(function(fun_name, fun_args, src_file) {
source(src_file)
do.call(get(fun_name), fun_args)
}, list(fun_name, fun_args, src_file))
}
runProcess(
fun_name='my_bg_func',
fun_args=list(par='value1'),
src_file='~/functions_source.R')
#Sys.sleep(35) # only with this does it work
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
reprexneeds a minimal reproducible exampleneeds a minimal reproducible example