Module pyfx.api.jobcontext

This module define the job context API.

This module contains the definition of the JobContext, exposing the available API, as well as some methods used to make a JobContext available globally.

The purpose of these "magic" method is to automatically provide an appropriate context to the user depending on the execution context. This allows the user to execute their code in different execution contexts transparently.

The way it works is by setting a global variable in the pyfx module. The context can be set externally. If not, on first access (with get_context()), an attempt to bootstrap a JobContext from a config file will be made. If unsuccessful, the access will fail with an error.

This allows us some niceties such as allowing tranparent local execution of a script for debugging.

Functions

def get_context() ‑> JobContext

Get current appropriate execution context.

Classes

class JobContext (connection: Connection, current_model: Optional[Model], current_job: Optional[PlatformJob], user_param: Dict[str, Any], logger: Optional[Callable[[Optional[str], Optional[int]], None]] = None)

Job execution context.

Static methods

def from_config(config: configparser.ConfigParser) ‑> JobContext

Create a JobContext from config.

Methods

def available_cpus(self) ‑> Optional[int]

Return the number of CPUs available from the environment.

Returns None if the number of CPUs is undertermined.

When running in a containerized environment, os.cpu_count can give unreliable result. This method takes in account such case to offer a more reliable value.

For more details, see https://bugs.python.org/issue36054

def current_job(self) ‑> Optional[PlatformJob]

Get the job associated with the context, if any.

def current_model(self) ‑> Optional[Model]

Get the model associated with the context, if any.

def instance(self) ‑> Instance

Get the Pricefx platform instance.

def log(self, msg: Optional[str] = None, progress: Optional[int] = None) ‑> None

Log some message and/or progress.

The actual logging method depends on the logging mechanism configured.

If an explicit logger is configured, it will be used. Else if the context contains a current job, it will be used. Else we do nothing.

Args

progress
some progress percent (optional)
msg
a message (optional)
def parameters(self) ‑> Dict[str, Any]

Get the parameters associated with the context.

Those custom parameters are accessible from the script.