简体   繁体   English

如何同时运行 2 个文件(.py)并将变量更新到另一个文件?

[英]How to run 2 files(.py) concurrently and update the variable to another file?

My definite goal is to update the real-time value to Matlab(simulink) from python to apply control system.我的明确目标是将实时值从 python 更新到 Matlab(simulink) 以应用控制系统。

With separated processes, I get the real-time updating value.通过分离的进程,我得到了实时更新值。 The value type is an integer.值类型是 integer。

I want to pass this updating value to Matlab workspace.我想将此更新值传递给 Matlab 工作区。 So I tried using the command in Matlab workspace: pyrunfile('A.py')所以我尝试在 Matlab 工作区中使用命令: pyrunfile('A.py')

However, As you see this link, 10th line of "Limitations to Python Support", https://fr.mathworks.com/help/matlab/matlab_external/limitations-to-python-support.html但是,正如您看到此链接,“Python 支持的限制”的第 10 行, https://fr.mathworks.com/help/matlab/matlab_external/limitations-to-python-269A239EZ308AD

Matlab doesn't support multiprocessing. Matlab 不支持多处理。 In other words, if I try to run the python file from Matlab workspace, it doesn't work.换句话说,如果我尝试从 Matlab 工作区运行 python 文件,它就不起作用。

But multiprocessing is requisite for my work.但我的工作需要多处理。 (not working with multithread) (不适用于多线程)

So my idea:所以我的想法:

  1. Run the file A.py which contains multiprocessing.运行包含多处理的文件 A.py。
  2. under A.py is still running, I pass the desired updating value to another file B.py with loop.在 A.py 仍在运行下,我将所需的更新值通过循环传递给另一个文件 B.py。
  3. Export this value to Matlab workspace.将此值导出到 Matlab 工作区。
  4. Matlab workspace -> simulink Matlab 工作区-> simulink

Firstly, I would like to know whether it sounds feasible or not.首先,我想知道这听起来是否可行。 if not, I would like to have some other workflow suggestion.如果没有,我想有一些其他的工作流程建议。

summary:概括:

python -> matlab is not possible because of multiprocessing. python -> matlab 是不可能的,因为多处理。

python ->?? python ->?? -> matlab, is there any other method? -> matlab,还有其他方法吗?

I'm not sure if this is the most efficient way, but you could write the variable to a file and read it from the other file.我不确定这是否是最有效的方法,但您可以将变量写入文件并从另一个文件中读取。

#Read file
with open("file.txt", "r") as txt_file:
    return txt_file.readlines()

#Open file
txt_file = open("file.txt", "w")
txt_file.write(var)
txt_file.close()

You can pass the values like that.您可以像这样传递值。 I'm not sure how to do the rest, but i hope this helps我不知道如何做 rest,但我希望这会有所帮助

Also just make 2 instances of the command line, and run the files seperately to run both of them也只需创建 2 个命令行实例,然后分别运行文件以运行它们

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM