MATLAB Engine API for Python: Persistent Session vs. Per-Call Startup for Low-Traffic Workloads
0 reputation · 07 Jun 2024, 14:09 UTC
0 reputation · 07 Jun 2024, 14:09 UTC
The MATLAB Engine API for Python connects a Python client process with a MATLAB runtime session. For workloads that invoke MATLAB functions infrequently, the session lifecycle becomes a design decision at this boundary.
Determine whether to maintain a single persistent MATLAB Engine session across sparse calls or to start and terminate a session for each call. MATLAB desktop licensing is user- and core-based, not traffic-based, so cost reduction does not come from session count. However, an idle Engine session retains a MATLAB process, holding memory and CPU resources. Conversely, repeated cold starts add latency (typically several seconds) and may increase license checkout frequency.
matlab.engine.start_matlab in the target MATLAB release and Python version?34275 reputation · 07 Jun 2024, 16:47 UTC
For workloads that invoke MATLAB functions only a handful of times per day, keeping a single persistent matlab.engine session is generally preferable. The initial cold‑start latency (~3–5 s) is avoided, the MATLAB process remains checked‑out so you avoid repeated license server traffic, and the memory footprint (≈200–400 MB) is modest compared to the startup cost.
matlab.engine.start_matlab() in recent releases averages 3–5 seconds on typical workstations.eng.eval('clear all', nargout=0)
eng.quit() to release the license and terminate the MATLAB process.import time, matlab.engine
start = time.perf_counter()
eng = matlab.engine.start_matlab()
print('startup', time.perf_counter()-start)
eng.quit()
psutil:
import psutil
eng = matlab.engine.start_matlab()
pid = eng._process.pid
print('RAM', psutil.Process(pid).memory_info().rss / (1024**2), 'MB')
eng.quit()
lmstat before and after starting/quitting to confirm checkout behaviour.To fine‑tune the recommendation, please confirm whether your environment uses a shared license server with per‑user limits. If so, persistent sessions help avoid hitting those limits.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.