我开发了Python代码,版本3.8.5,通过它调用R2018a Matlab使用matlab引擎函数,使用ubuntu 20.04处理数据。我使用了多处理库中的pool命令,在使用Matlab引擎加速处理的同时,我能够使用20个线程。代码运行良好,我能够多次获得结果。然而,我已经将Matlab升级到R2022b版本,现在使用完全相同的代码,我们面临着在池内调用MATLAB引擎的问题。代码在到达Matlab引擎函数时卡住,甚至没有打开Matlab函数来处理数据。它被卡在“启动Matlab引擎”,无法继续。我使用的是jueyter notebook,但是,我在终端中使用.py尝试了我的代码,仍然得到了相同的结果。我创建了一个示例代码,如下所示,供您审阅。请注意,Matlab引擎在单独调用时工作得很好。此外,池在单独调用时也能按预期工作。但是,当在池中调用Matlab引擎时,代码会卡住。我很感激任何意见或反馈,以帮助我解决这个问题。
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.
1条答案
按热度按时间pgvzfuti1#
如果这个问题仍然相关的话。我也有类似的问题,对我来说,它有助于在多进程函数调用中启动和结束matlab引擎。
每个进程调用多个matlab执行的伪代码看起来像这样: