简体   繁体   中英

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.

With separated processes, I get the real-time updating value. The value type is an integer.

I want to pass this updating value to Matlab workspace. So I tried using the command in Matlab workspace: 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

Matlab doesn't support multiprocessing. In other words, if I try to run the python file from Matlab workspace, it doesn't work.

But multiprocessing is requisite for my work. (not working with multithread)

So my idea:

  1. Run the file A.py which contains multiprocessing.
  2. under A.py is still running, I pass the desired updating value to another file B.py with loop.
  3. Export this value to Matlab workspace.
  4. Matlab workspace -> 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, is there any other method?

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

Also just make 2 instances of the command line, and run the files seperately to run both of them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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