setup
Worker startup helpers (internal).
This module contains the worker-side initialization routines that must run inside the spawned worker process before any heavy brms/rpy2 work:
- apply "safer embedded R" configuration (ABI mode, disabling unsafe fork parallelism)
- optionally run R startup scripts (user-configured)
- activate/deactivate a brmspy runtime and prepend the environment user library to
R
.libPaths().
The main process should not import rpy2; all embedded-R initialization happens here.
Attributes¶
__all__ = ['run_startup_scripts', '_check_r_setup', '_initialise_r_safe', 'activate']
module-attribute
¶
Classes¶
EnvironmentConfig
dataclass
¶
Worker environment configuration.
This configuration is applied in the worker before importing/using brms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r_home
|
str or None
|
Override for |
None
|
startup_scripts
|
list[str]
|
R code snippets executed in the worker after initialization. |
list()
|
environment_name
|
str
|
brmspy environment name (used to determine |
'default'
|
runtime_path
|
str or None
|
Path to a brmspy runtime bundle to activate in the worker. |
None
|
env
|
dict[str, str]
|
Extra environment variables applied when spawning the worker. |
dict()
|
Source code in brmspy/types/session.py
Attributes¶
r_home = None
class-attribute
instance-attribute
¶
startup_scripts = field(default_factory=list)
class-attribute
instance-attribute
¶
environment_name = 'default'
class-attribute
instance-attribute
¶
runtime_path = None
class-attribute
instance-attribute
¶
env = field(default_factory=dict)
class-attribute
instance-attribute
¶
Functions¶
to_dict()
¶
Serialize configuration for persistence to JSON.
Source code in brmspy/types/session.py
from_dict(obj)
classmethod
¶
Deserialize configuration from a JSON object.
Source code in brmspy/types/session.py
from_obj(obj)
classmethod
¶
Normalize None | dict | EnvironmentConfig into an EnvironmentConfig.
Source code in brmspy/types/session.py
__init__(r_home=None, startup_scripts=list(), environment_name='default', runtime_path=None, env=dict())
¶
Functions¶
get_environment_userlibs_dir(name)
¶
run_startup_scripts(env_conf)
¶
Execute configured R startup scripts in the worker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_conf
|
EnvironmentConfig
|
Environment configuration. If |
required |
Source code in brmspy/_session/worker/setup.py
_check_r_setup(verbose=False)
¶
Run lightweight diagnostics to detect common embedded-R misconfiguration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
If True, prints human-readable info/warnings. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[bool, list[str]]
|
|
Source code in brmspy/_session/worker/setup.py
_initialise_r_safe()
¶
Configure R for safer embedded execution.
- Try to enforce rpy2 ABI mode (must be set before importing rpy2)
- Disable fork-based R parallelism (future::multicore, mclapply)
- Use future::plan(sequential) if future is available
- Leave cmdstanr multi-core sampling alone
Source code in brmspy/_session/worker/setup.py
activate(env_conf)
¶
Apply runtime + environment library configuration in the worker.
This may activate/deactivate a brmspy runtime (prebuilt bundle) and ensures the
per-environment user library is at the front of R .libPaths().
Notes
This is intentionally worker-only: it imports runtime helpers and mutates the embedded R process state.