Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

python - How to kill a MATLAB Batch Process ran by Qt QProcess?

I currently have a Python program that calls a MATLAB script as batch like so:

matlab = QProcess()
matlab.start('matlab -noFigureWindows -batch "cd(users/script_directory/); MyScript.m;"')
#^ command to start MATLAB batch process in CMD

The issue I'm running into is that once this batch process starts, there's no way to kill it. So if my Python app gets force-closed, the MATLAB script keeps running and causes all sorts of issues, meaning I need to kill the process on app close.

I'm calling the MATLAB script as a QProcess and get the following message when I force-close the Python app before the MATLAB script finishes executing:

QProcess: Destroyed while process ("matlab") is still running.

With this, how do I stop the batch MATLAB process? Using 'ctrl-c' in CMD works for me sometimes to kill the process but I need it to be consistent to make the Python work right.

Similarly, can I just have it 'force quit' or 'restart' batch MATLAB or anything along those lines to clear all running processes?

question from:https://stackoverflow.com/questions/65648588/how-to-kill-a-matlab-batch-process-ran-by-qt-qprocess

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A brute force way to kill it would be to just kill any matlab process via the process and system utilities library at the start of your application:

import psutil

for process in psutil.process_iter():
    if process.name().lower() == 'matlab.exe':
        process.terminate()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...