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

6 visualizaciones (últimos 30 días)
Giacomo
Giacomo el 2 de Sept. de 2022
Respondida: Bo Li el 8 de Sept. de 2022
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.

Respuestas (1)

Bo Li
Bo Li el 8 de Sept. de 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.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by