From henrik.bengtsson at gmail.com  Wed Jan 25 04:06:03 2017
From: henrik.bengtsson at gmail.com (Henrik Bengtsson)
Date: Tue, 24 Jan 2017 19:06:03 -0800
Subject: [Rd] parallel::mc*: Is it possible for a child process to know it
	is a fork?
Message-ID: <CAFDcVCRtpGfSvUxyQKSoufWLCAeXVCTMpn6x9WFL9F1SJh68AA@mail.gmail.com>

When using multicore-forking of the parallel package, is it possible
for a child process to know that it is a fork?  Something like:

  parallel::mclapply(1:10, FUN = function(i) { test_if_running_in_a_fork() })

I'm looking into ways to protect against further parallel processes
(including threads), which not necessarily are created via the
parallel:mc* API, are being spawned off recursively.  For instance,
there are several packages that by default perform multi-threaded
processing using native code, but I'm not sure there's a way for such
package to avoid running in multi-threaded mode if running in a forked
child R processes.  Imagine

  y <- parallel::mclapply(1:10, FUN = function(i) {
     somepkg::threaded_calculation_using_all_cores()
  })

where the developer of `somepkg` is off no control whether user calls
it via mclapply() or via lapply().   I can see how the user of
mclapply() / lapply() can pass on this information, but that's not
safe and it might not be that the user is aware that deep down in the
dependency hierarchy there's one or more functions that do
multi-thread/process processing.

Thanks,

Henrik



[Rd] parallel::mc*: Is it possible for a child process to know it is a fork?
Henrik Bengtsson