mosaik.starters — Descriptions of how to start simulators
Starters describe how mosaik instantiates or connects to simulators.
This module provides the abstract base class Starter, and the
three concrete subclasses PythonStarter for starting a
simulator by instantiating a Simulator subclass
in the running process, CmdStarter for starting a simulator
by spawning a separate process, and ConnectStarter for
connecting to a simulator running at some TCP/IP address.
These starters can be used by storing them in a SIM_CONFIG dict
mapping simulator names to Starter objects. When such a
SIM_CONFIG is given to the mosaik World at
creation, instances of the simulators can be spawned by simply giving
the simulator name to the worlds mosaik.World.start method.
Alternatively, this method also accepts a Starter object
directly. (In this case, you need to specify the simulator ID, as it
cannot be auto-generated from the simulator name.)
Finally, traditionally, SIM_CONFIG would be a dict of dicts, where
the inner dicts correspond to our Starter objects. To keep
supporting this, Starter objects can be parsed from such a dict
using the from_starter_config method; to try parsing
into all starters automatically, use
get_starter_from_starter_config.
- class Starter[source]
Description of how to start or connect to a mosaik simulator.
In practice, you will usually use one of the subclasses
PythonStarter,CmdStarter, orConnectStarter.- api_version: str | None[source]
The API version of this simulator. This should be set if the simulator is using an outdated version of the API.
- abstractmethod async start(sim_id: mosaik_api_v3.SimId, mosaik_remote: MosaikRemote, mosaik_config: MosaikConfigTotal) BaseProxy[source]
Start the simulator as described by this Starter under the name
sim_idand using the suppliedmosaik_remoteto allow it to make callbacks to mosaik.This may raise
ScenarioError(or appropriate subclasses) if the simulator cannot be started.- Parameters:
sim_id (mosaik_api_v3.SimId)
mosaik_remote (MosaikRemote)
mosaik_config (MosaikConfigTotal)
- Return type:
BaseProxy
- abstractmethod classmethod from_starter_config(starter_config: StarterConfig) Starter | None[source]
Attempt to create a starter from the given StarterConfig. If the StarterConfig does not match this type of Starter, return
Noneto indicate that a different Starter should be tried.- Parameters:
starter_config (StarterConfig)
- Return type:
Starter | None
- classmethod from_sim_config_entry(entry: StarterConfig | Starter) Starter[source]
Create a
Starterfrom an entry in aSimConfig. This is intended to be called on a subclass ofStarterand will check that the givenentryis either already an instance of that subclass or that it can be parsed into one by that subclass’sfrom_starter_configmethod.This is a convenience method if you already have a
SimConfig. In most cases, you will either construct starters directly or useget_starter_from_starter_config, instead.
- class PythonStarter(cls: type[Simulator], *, api_version: str | None = None, args: tuple[Any, ...] = (), kwargs: dict[str, Any] = {})[source]
Description of how to start a simulator based on its mosaik_api_v3.Simulator class.
In traditional mosaik, this starter corresponds to a
"python"entry in theSimConfig, and it can be constructed from such an entry usingfrom_starter_config.- Parameters:
- cls: type[Simulator][source]
The
Simulatorsubclass started by this starter. When started, the class’s constructor will be called withargsandkwargs.
- async start(sim_id: mosaik_api_v3.SimId, mosaik_remote: MosaikRemote, mosaik_config: MosaikConfigTotal) BaseProxy[source]
Start the simulator as described by this Starter under the name
sim_idand using the suppliedmosaik_remoteto allow it to make callbacks to mosaik.This may raise
ScenarioError(or appropriate subclasses) if the simulator cannot be started.- Parameters:
sim_id (mosaik_api_v3.SimId)
mosaik_remote (MosaikRemote)
mosaik_config (MosaikConfigTotal)
- Return type:
BaseProxy
- classmethod from_module_class_name(mod_name: str, cls_name: str, *, api_version: str | None = None) PythonStarter[source]
Attemp to import the simulator class
cls_namefrom the modulemod_name, and return aPythonStarterusing this class if successful.- Parameters:
- Return type:
- classmethod from_string(import_string: str, *, api_version: str | None = None) PythonStarter[source]
Attempt to import the a simulator class based on
import_string: It should follow the form “module_name:ClassName” (note the colon instead of a dot). Return aPythonStarterif successful.- Parameters:
- Return type:
- classmethod from_starter_config(starter_config: StarterConfig) Starter | None[source]
Attempt to create a starter from the given StarterConfig. If the StarterConfig does not match this type of Starter, return
Noneto indicate that a different Starter should be tried.- Parameters:
starter_config (StarterConfig)
- Return type:
Starter | None
- class CmdStarter(cmd: str, *, api_version: str | None = None, auto_terminate: bool = True, bind_addr: tuple[str, int | None] | None = None, connect_timeout: float | None = None, cwd: str = '.', env: dict[str, str] = {}, new_console: bool = False, posix: bool = False)[source]
Description of how to start a simulator in a new process.
In traditional mosaik, this starter corresponds to a
"cmd"entry in theSimConfig, and it can be constructed from such an entry usingfrom_starter_config.- Parameters:
- auto_terminate: bool[source]
Whether to automatically terminate the process when the world is shut down
- async start(sim_id: mosaik_api_v3.SimId, mosaik_remote: MosaikRemote, mosaik_config: MosaikConfigTotal) BaseProxy[source]
Start the simulator as described by this Starter under the name
sim_idand using the suppliedmosaik_remoteto allow it to make callbacks to mosaik.This may raise
ScenarioError(or appropriate subclasses) if the simulator cannot be started.- Parameters:
sim_id (mosaik_api_v3.SimId)
mosaik_remote (MosaikRemote)
mosaik_config (MosaikConfigTotal)
- Return type:
BaseProxy
- classmethod from_starter_config(starter_config: StarterConfig) Starter | None[source]
Attempt to create a starter from the given StarterConfig. If the StarterConfig does not match this type of Starter, return
Noneto indicate that a different Starter should be tried.- Parameters:
starter_config (StarterConfig)
- Return type:
Starter | None
- class ConnectStarter(host: str, port: int, *, api_version: str | None = None)[source]
Description of how to “start” a simulator already running at some address by connecting to it.
In traditional mosaik, this starter corresponds to a
"connect"entry in theSimConfig, and it can be constructed from such an entry usingfrom_starter_config.- async start(sim_id: mosaik_api_v3.SimId, mosaik_remote: MosaikRemote, mosaik_config: MosaikConfigTotal) BaseProxy[source]
Start the simulator as described by this Starter under the name
sim_idand using the suppliedmosaik_remoteto allow it to make callbacks to mosaik.This may raise
ScenarioError(or appropriate subclasses) if the simulator cannot be started.- Parameters:
sim_id (mosaik_api_v3.SimId)
mosaik_remote (MosaikRemote)
mosaik_config (MosaikConfigTotal)
- Return type:
BaseProxy
- classmethod from_addr_string(address: str, *, api_version: str | None = None) ConnectStarter[source]
Construct a
ConnectStarterfrom an address string in the format “host:port”.- Parameters:
- Return type:
- classmethod from_addr(addr: str | tuple[str, int], *, api_version: str | None = None) ConnectStarter[source]
Construct a
ConnectStarterfrom a host-port pair.
- classmethod from_starter_config(starter_config: StarterConfig) Starter | None[source]
Attempt to create a starter from the given StarterConfig. If the StarterConfig does not match this type of Starter, return
Noneto indicate that a different Starter should be tried.- Parameters:
starter_config (StarterConfig)
- Return type:
Starter | None
- STARTERS: list[type[Starter]] = [<class 'mosaik.starters.PythonStarter'>, <class 'mosaik.starters.CmdStarter'>, <class 'mosaik.starters.ConnectStarter'>][source]
The default starters used by mosaik.
You can add additional
Startersubclasses to this list to make parsing their starter configs availabe in yourSimConfig. Alternatively, your own starters can also be used as entries in theSimConfigdirectly, or be passed toworld.startdirectly.
- get_starter_from_starter_config(starter_config: StarterConfig) Starter[source]
Construct a
Starterfrom the givenStarterConfigby trying theStartersubclasses inSTARTERSone by one (using theStarter.from_starter_configmethod from each).- Parameters:
starter_config (StarterConfig)
- Return type: