Skip to content

How to tell if you are already within %dopar% loop? #35

Description

@JSchoenbachler

Just seeing if there is a sure fire way to determine if you are already within a foreach loop using %dopar%? As it is currently, I have a workaround function that parses sys.status() for any foreach calls that also use %dopar%-

isAlreadyInParallel = function() {
  status = sys.status()
  return(any(grepl(
    '%dopar%',
    status$sys.calls[grepl('foreach(', status$sys.calls, fixed = TRUE)],
    fixed = TRUE)))}

However, this feels pretty limited and doesn't cover the case in which someone uses a variable to hold %dopar% like below-

doFunction = if (foreach::getDoParRegistered()) `%dopar%` else `%do%`

doFunction(foreach(1:5), {...})

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions