Last active
October 23, 2020 21:09
-
-
Save espdev/30862cd339803925b15801ec1ca35af4 to your computer and use it in GitHub Desktop.
Using Matlab shared 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
# -*- coding: utf-8 -*- | |
import multiprocessing as mp | |
import matlab.engine as me | |
def start_matlab(ready_event, close_event): | |
print('Starting matlab...') | |
eng = me.start_matlab(option='-nodesktop') | |
eng.matlab.engine.shareEngine(nargout=0) | |
name = eng.matlab.engine.engineName(nargout=1) | |
print('MATLAB engine name:', name) | |
ready_event.set() | |
close_event.wait() | |
print('Exiting and closing Matlab...') | |
def main(): | |
ready_event = mp.Event() | |
close_event = mp.Event() | |
ready_event.clear() | |
close_event.clear() | |
p = mp.Process(target=start_matlab, args=(ready_event, close_event)) | |
p.daemon = False | |
p.start() | |
ready_event.wait() | |
names = me.find_matlab() | |
print('Found matlab sessions:', names) | |
print('Connect to Matlab:', names[0]) | |
try: | |
eng = me.connect_matlab(names[0]) | |
except Exception: | |
raise | |
else: | |
print(eng.version(nargout=1)) | |
finally: | |
close_event.set() | |
if __name__ == '__main__': | |
mp.set_start_method('spawn') # for working on unix | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment