简体   繁体   English

通过本机消息扩展 (Firefox) 从 Python 启动 Matlab 时出现奇怪的错误

[英]Strange error when starting Matlab from Python via Native Messaging Extension (Firefox)

I am trying to run some Matlab Code on an URL that I retrieve from a Firefox Native Messaging app.我正在尝试在从 Firefox Native Messaging 应用程序检索的 URL 上运行一些 Matlab 代码。 I use a python script that uses the Matlab engine to run my scripts.我使用 python 脚本,该脚本使用 Matlab 引擎来运行我的脚本。 This works perfectly fine:这工作得很好:

import matlab.engine
import sys
import struct
import time
import subprocess
import logging

# Function to receive Message
def getMessage():
    rawLength = sys.stdin.buffer.read(4)
    print(rawLength)
    if len(rawLength) == 0:
        sys.exit(0)
    messageLength = struct.unpack('@I', rawLength)[0]
    message = sys.stdin.buffer.read(messageLength).decode('utf-8')

    eng = matlab.engine.connect_matlab()
    eng.workspace['url'] = message
    eng.eval('myMatlabFunc(url)', nargout=0)


getMessage()

Now I want to check if Matlab is running and if not start it:现在我想检查 Matlab 是否正在运行,如果没有启动它:

if not matlab.engine.find_matlab():
    subprocess.run('C:\\Program Files\\MatlabR2020b-64bit\\bin\\matlab.exe')

This works and I can run my code afterwards as well:这很有效,之后我也可以运行我的代码:

eng = matlab.engine.connect_matlab()
eng.workspace['var1'] = '1'

But then I encounter a strange behaviour, that I cant explain.但是后来我遇到了一种奇怪的行为,我无法解释。

As soon as the script is finished, Matlab exits without a Warning/Crash/Exception... The weirdest part is, that this only happens if I run the skript via the native messaging connection.脚本完成后,Matlab 将退出而没有警告/崩溃/异常...最奇怪的是,只有当我通过本机消息连接运行脚本时才会发生这种情况。

If I run the script in Pycharm, everythin works as expected.如果我在 Pycharm 中运行脚本,一切都会按预期工作。

I hope someone here can help me figure this out!我希望这里有人可以帮助我解决这个问题!

Thanks, Ben谢谢,本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启动Firefox时出现python webdriver错误 - python webdriver error when starting Firefox 通过 Python 在 MPI_Init 中启动 Open MPI 时出错 - Error when starting Open MPI in MPI_Init via Python 通过命令行从 python 调用 java 程序的奇怪错误 - Strange error calling java program from python via command line 通过subprocess.Popen执行Matlab例程时如何从Matlab向Python返回值 - How to return values from Matlab to Python when executing a Matlab routine via subprocess.Popen 错误:通过 python 子进程调用启动 NodeJS 服务器时找不到模块 - Error: Cannot find module when starting NodeJS server via python subprocess call 为什么在通过 conda 启动 jupyter notebook 时出现 python37.dll 错误 - Why do I get a python37.dll error when starting jupyter notebook via conda 从 Python 运行 Matlab 脚本时如何获取 Matlab 错误消息 - How to get Matlab error message when running a Matlab script from Python 从 Python 2 移植到 Python 3 时,本机消息传递主机 (Chrome) 无法正确接收消息 - Native Messaging Host (Chrome) failing on receiving messages properly when ported form Python 2 to Python 3 在 Python 3.10 上启动 Spyder IDE 时出错 - Error when starting Spyder IDE on Python 3.10 从 Python 开始:进行 tplquad 集成时出错 - Starting on Python : Error when doing a tplquad integration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM