I developed Python code, version 3.8.5, through which I called R2018a Matlab using matlab engine function to process the data using ubuntu 20.04. I used the pool command from the multiprocessing library and I was able to use 20 threads while using the Matlab engine to speed up the process. The code was working fine and I was able to obtain results multiple times. However, I have upgraded Matlab to the R2022b version and now using the exact same code, we are facing an issue with the MATLAB engine being called inside the pool. The code stuck when it reaches the Matlab engine function, and it doesn’t even open the Matlab function to process data. It gets stuck at "Starting Matlab Engine" and does not proceed. I am using Jupyter notebook, however, I have tried my code using the .py inside the terminal and still got the same result. I have created an example code as follows for your review. Please note that the Matlab engine works perfectly fine when it is being called separately. Also, the pool is working as expected when called separately. However, the code gets stuck when the Matlab engine is called inside the pool. I’d appreciate any comment or feedback to assist me to tackle the issue.
import multiprocessing
from multiprocessing import Pool
import matlab.engine
import time
print("defining function ...")
def function88 (j):
print("cal result1 ... ")
result1=j**2
print(result1)
print("Starting Matlab engine...")
eng = matlab.engine.start_matlab()
print("Matlab engine started...")
print("Calling Matlab function...")
out = eng.test(j)
print("Matlab function output:")
print(out)
return result1
print("calling pool ...")
pool = multiprocessing.Pool(1)
print("calling pool.map")
time.sleep(3)
result = pool.map(function88, range(10))
print("printing result")
print (result)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The result is as follows:
defining function ...
calling pool ...
calling pool.map
cal result1 ...
0
Starting Matlab engine...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My expectation was to see the Matlab function output.