Weird behaviour of py.importlib when using "InProcess" ExecutionMode

5 views (last 30 days)
I am building a MATLAB script which calls some third part Python libraries I have installed on my machine. I am facing a weird behavior. My code is as simple as
mylib=py.importlib.import_module("third_part_library");
where "third_part_library" is the name of the python module I want to import.
When running it with ExecutionMode set to "InProcess", MATLAB opens a new instance, where I get a warning in command window.
Warning: Unrecognized command line option: m.
Warning: Unrecognized command line option: -version.
This new instance appears to do nothing, as I have the "Ready" text in the bottom left. The original instance is stuck to "Busy" until I close the new one. Once the second instance is closed, a third one appears, with the same warning as before. Once I close this third instance, the original instance completes the import successfully.
I am trying to understand if this behaviour is due to MATLAB or not. I tried changing "ExecutionMode" to "OutOfProcess", and I do not get the same issue (even if I can see a black cmd window flashing for a second). I cannot keep the "OutOfProcess" setting due to performance issue, as my actual script is way slower than it would be using "InProcess" (of a factor of 10).
Am I doing something wrong or is it a weird interaction with python libraries? Thanks in advance to anyone willing to help.

Answers (1)

Bo Li
Bo Li on 8 Sep 2022
It appears that "third_part_library" happens to use the Python multiprocessing package. In such case, the Python module creates another process which will be MATLAB since MATLAB and Python are sharing the same process for the in-process mode. For the out-of-process mode, it will be another process called MATLABPyHost. In either mode, the usage of inter-process communication might not be included in the design of Python Interface. The development team is aware of this issue and would consider it for a future version of MATLAB.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!