-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from rascal2.core.runner import RATRunner | ||
from rascal2.core.settings import Settings, get_global_settings | ||
from rascal2.core.matlab import MatlabHandler | ||
|
||
__all__ = ["RATRunner", "get_global_settings", "Settings"] | ||
__all__ = ["MatlabHandler", "RATRunner", "get_global_settings", "Settings"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""MATLAB engine that runs in the background.""" | ||
|
||
from contextlib import suppress | ||
|
||
|
||
class MatlabHandler: | ||
"""A singleton class that provides a MATLAB engine.""" | ||
|
||
_instance = None | ||
|
||
def __init__(self): | ||
raise RuntimeError("MatlabHandler should not be invoked directly. " "Use MatlabHandler.instance().") | ||
|
||
@classmethod | ||
def instance(cls): | ||
"""Instantiate a MatlabHandler if one does not exist, or return the existing one if one exists.""" | ||
if cls._instance is None: | ||
cls._instance = cls.__new__(cls) | ||
cls._instance.init() | ||
return cls._instance | ||
|
||
def init(self): | ||
"""Instantiate the MatlabHandler.""" | ||
self.future = None | ||
self.engine = None | ||
with suppress(ImportError): | ||
import matlab.engine | ||
|
||
self.future = matlab.engine.start_matlab(background=True) | ||
|
||
def get_engine(self): | ||
"""Get the MATLAB engine.""" | ||
if self.engine is not None: | ||
return self.engine | ||
|
||
if self.future is None: | ||
raise ImportError("Attempted to start MATLAB engine, but `matlabengine` is not installed!") | ||
|
||
self.engine = self.future.result() | ||
return self.engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters