Process Control

Process launching and querying

class mhi.pscad.common.process.MIB_TCPROW_LH
class mhi.pscad.common.process.MIB_TCPROW_OWNER_PID
mhi.pscad.common.process.process_pids(*names: str) → List[Tuple[str, int]]

Return the process id’s of any process with the given executable names.

Application names may include the % wildcard. For example, the following query might find both SkypeApp.exe and SkypeBackgroundHost.exe:

process_ids('skype%.exe')

Since applications may terminate and can be started at any time, the returned value is obsolete immediately upon being returned.

Parameters

*names (str) – application filename patterns, without any path.

Returns

A list of process name & process id pairs

Return type

List[tuple]

mhi.pscad.common.process.is_running(app_name: str) → bool

Determine if there is an app process in the list of running processes.

Application names may include the % wildcard. For example, the following query might find both SkypeApp.exe and SkypeBackgroundHost.exe:

is_running('skype%.exe')

Since applications may terminate and can be started at any time, the returned value is obsolete immediately upon being returned.

Parameters

app (str) – application filename, without any path.

Returns

True if a process can be found, False otherwise.

Return type

bool

mhi.pscad.common.process.tcp_ports_in_use() → Set[int]

Find all TCP ports in use

Returns

Set of all ports in use by the TCP protocol

Return type

Set[int]

mhi.pscad.common.process.unused_tcp_port() → int

Find an available TCP ports in the dynamic/private range

Returns

an available TCP port

Return type

int

mhi.pscad.common.process.listener_ports_by_pid(*pid: int) → List[Tuple[str, int, int]]

Find all listener ports opened by processes with the given PIDs.

Since applications may terminate and can be started at any time, as well as open and close ports at any time, the returned value is obsolete immediately upon being returned.

Parameters

*pid (int) – Process ids

Returns

a list of (addr, port, pid) tuples

Return type

List[tuple]

mhi.pscad.common.process.listener_ports_by_name(*names: str) → List[Tuple[str, int, int, str]]

Find all listener ports opened by processes with the given executable name.

Application names may include the % wildcard. For example, the following query might find both listener ports opened by both SkypeApp.exe and SkypeBackgroundHost.exe:

listener_ports_by_name('skype%.exe')

Since applications may terminate and can be started at any time, as well as open and close ports at any time, the returned value is obsolete immediately upon being returned.

Parameters

*names (str) – application filename patterns, without any path.

Returns

a list of (addr, port, pid, name) tuples

Return type

List[tuple]

mhi.pscad.common.process.versions(app_name: str) → List[Tuple[str, bool]]

Find the installed versions of an MHI application.

Returns

List of tuples of version and bit-size

Return type

List[Tuple]

mhi.pscad.common.process.find_exe(app_name: str, version: Optional[str] = None, x64: Optional[bool] = None, minimum: Optional[str] = None, maximum: Optional[str] = None, allow_alpha: bool = True, allow_beta: bool = True) → Optional[str]

Find an MHI application executable.

If no version is specified, the highest version available is used, with Alpha and Beta versions being considered the highest and second highest respectively. If no x64 flag is given, a 32-bit or 64-bit version may be returned, with preference given to 64-bit versions.

Parameters
  • app (str) – name of the application (without any extension)

  • version (str) – application version number such as ‘4.6.3’ or ‘Alpha’

  • x64 (bool) – True for 64-bit version, False for 32-bit version

  • minimum (str) – The lowest allowable version, such as ‘5.0’

  • maximum (str) – The highest allowable version, such as ‘4.6.9’

  • allow_alpha (bool) – Set to False to exclude alpha versions

  • allow_beta (bool) – Set to False to exclude beta versions

Returns

the path to the executable

Return type

str

New in version 2.1: minimum, maximum, allow_alpha & allow_beta parameters.

mhi.pscad.common.process.launch(*args: str, options: Dict[str, Any] = None, **kwargs)

Launch an application process.

All {keyword} format codes in the list of args strings are replaced by the value in the corresponding options dictionary and/or kwargs key-value argument pairs.

For example:

launch("C:\{dir}\{name}.exe", "/silent:{silent}", "/title:{title!r}",
       dir="temp", name="app", silent=True, title="Hello world")

will launch C:\temp\app.exe passing the arguments /silent:True and /title:'Hello world'

Parameters
  • *args (str) – the application and the arguments for the application

  • options – values which may be substituted in the application arguments

  • **kwargs – additional substitution values

Returns

The subprocess handle

Special keyword arguments

Keyword=Value

Effect

minimize=True

process started with SW_SHOWMINNOACTIVE

minimize=False

process started with SW_SHOWNOACTIVATE

debug=True

process is not started, command line printed