later icon indicating copy to clipboard operation
later copied to clipboard

Call the callback function at a specific call stack depth?

Open Jiefei-Wang opened this issue 2 years ago • 0 comments

Hi, I'm a developer of a parallel backend package and looking for a way to call a function after a specific time period in the worker, so it can give me the chance to check the manager's status and interrupt the long-running task if needed. I did a little search and found your package is very interesting and related. I see that the callback only works at the top-level prompt, but in my case, the worker must be trapped in an infinite loop so the top-level prompt is not reachable. Here is an illustrative example for the worker

start_worker <- function(){
    while(TRUE){
        ## receive the job from the manager
        task <- receive()
        ## run the task
        ## need to check the manager's status when running the task
        result <- exec(task)
        ## send the result back
        send(result)
    }
}

Once the worker has been started, it will never return the control to the user, so there is no way to make the callback work in this case. However, I see in your document the reason for only calling the callback at the top level is for avoiding bugs due to reentrancy. This makes me wonder whether it is possible to call the callback at a specific call stack level if I know it is safe to call(e.g. only call the callback when the call stack is start_worker -> exec), or is it totally impossible due to the restriction of the R language? Could you please shed some light on this question? Any suggestion will be appreciated!

Best, Jiefei

Jiefei-Wang avatar Aug 02 '21 17:08 Jiefei-Wang