os_utils
Operating system utilities in the PEST(++) realm
PortManager
Bases: object
Cross-platform port manager for parallel processes.
__init__(port_range=(4004, 4999), lock_dir=None, max_retries=50, lock_timeout=5, log_level=logging.INFO)
Initialize the port manager. Args: port_range: Tuple of (min_port, max_port) to search within lock_dir: Directory to store lock files (default: system temp dir) max_retries: Maximum attempts to find an available port lock_timeout: Time in seconds after which a lock is considered stale
get_available_port()
Find and reserve an available port. Returns: An available port number. Raises: RuntimeError: If no available port can be found after max_retries.
reserved_port()
Context manager that reserves a port and releases it after use.
PyPestWorker
Bases: object
a pure python worker for pest++. the pest++ master doesnt even know...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pst
|
str or Pst
|
something about a control file |
required |
host
|
str
|
master hostname or IPv4 address |
required |
port
|
int
|
port number that the master is listening on |
required |
timeout
|
float
|
number of seconds to sleep at different points in the process. |
0.25
|
verbose
|
bool
|
flag to echo what's going on to stdout |
True
|
socket_timeout
|
float
|
number of seconds that the socket should wait before giving up. generally, this can be a big number... |
None
|
run(cmd_str, cwd='.', verbose=False, use_sp=False, **kwargs)
main run function so both run_sp and run_ossystem can coexist
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_str
|
`str`
|
the str to execute with |
required |
cwd
|
`str`
|
the directory to execute the command in. Default is ".". |
'.'
|
verbose
|
`bool`
|
flag to echo to stdout the |
False
|
Notes:
by default calls run_ossystem which is the OG function from pyemu that uses os.system()
if use_sp is True, then run_sp is called which uses subprocess.Popen instead of os.system
Example:: pyemu.os_utils.run("pestpp-ies my.pst",cwd="template")
run_ossystem(cmd_str, cwd='.', verbose=False)
an OS agnostic function to execute a command line
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_str
|
`str`
|
the str to execute with |
required |
cwd
|
`str`
|
the directory to execute the command in. Default is ".". |
'.'
|
verbose
|
`bool`
|
flag to echo to stdout the |
False
|
Notes
uses platform to detect OS and adds .exe suffix or ./ prefix as appropriate
if os.system returns non-zero, an exception is raised
Example::
pyemu.os_utils.run("pestpp-ies my.pst",cwd="template")
run_sp(cmd_str, cwd='.', verbose=True, logfile=False, **kwargs)
an OS agnostic function to execute a command line with subprocess
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_str
|
`str`
|
the str to execute with |
required |
cwd
|
`str`
|
the directory to execute the command in. Default is ".". |
'.'
|
verbose
|
`bool`
|
flag to echo to stdout the |
True
|
shell
|
`bool`
|
flag to use shell=True in the |
required |
Notes
uses sp Popen to execute the command line. By default does not run in shell mode (ie. does not look for the exe in env variables)
start_workers(worker_dir, exe_rel_path, pst_rel_path, num_workers=None, worker_root='..', port=None, rel_path=None, local=True, cleanup=True, master_dir=None, verbose=False, silent_master=False, reuse_master=False, restart=False, ppw_function=None, ppw_kwargs={})
start a group of pest(++) workers on the local machine
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worker_dir
|
`str`
|
the path to a complete set of input files need by PEST(++). This directory will be copied to make worker (and optionally the master) directories |
required |
exe_rel_path
|
`str`
|
the relative path to and name of the pest(++) executable from within
the |
required |
pst_rel_path
|
`str`
|
the relative path to and name of the pest control file from within
|
required |
num_workers
|
`int`
|
number of workers to start. defaults to number of cores |
None
|
worker_root
|
`str`
|
the root directory to make the new worker directories in. Default is ".." (up one directory from where python is running). |
'..'
|
rel_path
|
`str`
|
the relative path to where pest(++) should be run from within the worker_dir, defaults to the uppermost level of the worker dir. This option is usually not needed unless you are one of those crazy people who spreads files across countless subdirectories. |
None
|
local
|
`bool`
|
flag for using "localhost" instead of actual hostname/IP address on
worker command line. Default is True. |
True
|
cleanup
|
`bool`
|
flag to remove worker directories once processes exit. Default is True. Set to False for debugging issues |
True
|
master_dir
|
`str`
|
name of directory for master instance. If |
None
|
verbose
|
`bool`
|
flag to echo useful information to stdout. Default is False |
False
|
silent_master
|
`bool`
|
flag to pipe master output to devnull and instead print a simple message to stdout every few seconds. This is only for pestpp Travis testing so that log file sizes dont explode. Default is False |
False
|
reuse_master
|
`bool`
|
flag to use an existing |
False
|
restart
|
`bool`
|
flag to add a restart flag to the master start. If |
False
|
ppw_function
|
function
|
a function pointer that uses PyPestWorker to execute model runs. |
None
|
ppw_kwargs
|
dict
|
keyword arguments to pass to |
{}
|
Notes
If all workers (and optionally master) exit gracefully, then the worker
dirs will be removed unless cleanup is False
Example::
# start 10 workers using the directory "template" as the base case and
# also start a master instance in a directory "master".
pyemu.helpers.start_workers("template","pestpp-ies","pest.pst",10,master_dir="master",
worker_root=".")