Skip to content

Commit

Permalink
Merge pull request #28362 from pshriwise/moose-control-env
Browse files Browse the repository at this point in the history
Allow `MooseControl` to inherit the current Python env
  • Loading branch information
loganharbour authored Aug 14, 2024
2 parents 9c87fde + 1fb28ce commit 3c9f2c0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions python/MooseControl/MooseControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class MooseControl:
def __init__(self,
moose_command: list[str] = None,
moose_port: int = None,
moose_control_name: str = None):
moose_control_name: str = None,
inherit_environment: bool = True):
"""Constructor
If "moose_port" is specified without "moose_command": Connect to the webserver at
Expand All @@ -55,6 +56,7 @@ def __init__(self,
moose_command (list[str]): The command to use to start the moose process
moose_port (int): The webserver port to connect to
moose_control_name (str): The name of the input control object
inherit_environment (bool): Whether or not the MOOSE command will inherit the current shell environment
"""
# Setup a basic logger
logging.basicConfig(level=logging.INFO,
Expand All @@ -76,6 +78,7 @@ def __init__(self,
self._moose_command = moose_command
self._moose_port = moose_port
self._moose_control_name = moose_control_name
self._inherit_environment = inherit_environment

# Set defaults
self._url = None
Expand Down Expand Up @@ -252,7 +255,7 @@ def initialize(self):

# Spawn the moose process
logger.info(f'Spawning MOOSE with command "{moose_command}"')
self._moose_process = self.spawnMoose(moose_command)
self._moose_process = self.spawnMoose(moose_command, self._inherit_environment)

# And setup the threaded reader that will pipe the moose process
# to the common logger
Expand Down Expand Up @@ -549,12 +552,13 @@ def getPostprocessor(self, name: str) -> float:
return value

@staticmethod
def spawnMoose(cmd: list[str]) -> subprocess.Popen:
def spawnMoose(cmd: list[str], inherit_environment: bool = True) -> subprocess.Popen:
"""Helper for spawning a MOOSE process that will be cleanly killed"""
popen_kwargs = {'stdout': subprocess.PIPE,
'stderr': subprocess.STDOUT,
'text': True,
'universal_newlines': True,
'bufsize': 1}
'bufsize': 1,
'env': os.environ if inherit_environment else None}

return subprocess.Popen(cmd, **popen_kwargs)

0 comments on commit 3c9f2c0

Please sign in to comment.