Skip to content

Running new background process from a parent process that dies before background process terminate #296

@diegoperoni

Description

@diegoperoni

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    reprexneeds a minimal reproducible example

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions