Calling python scripts from Matlab and go to the next line without waiting

25 次查看(过去 30 天)
Hi all, I have a python script which handle a motor. The motor has a position feedback, so the python script waits for the position to be reached, before quitting. I'm using Maltab in a big app, and I want to move the motor while some cameras are showing a preview in matlab. I'm trying by uysing pyrunfile,which is great, but the problem is that the app waits for the script to complete before moving to the next line. It is worth noting that the script does not provide any output. So now this is what i get:
  • start camera preview and see the live images
  • start the python script, the motor moves
  • the preview freezes untill the motor stops, then start again
while i would like to have
  • start camera preview and see the live images
  • start the python script, the motor start moving
  • the preview shows live images while the motor is moving
Can anyone help me? Is there a way to run the python script from matlab, forcing matlab not to wait for the script completion?

回答(1 个)

Suraj
Suraj 2023-3-29
Hi Paolo
It is my understanding that you want to run your python script in a non-blocking fashion. You can achieve this by performing your activity on a separate thread that runs in the background. Here’s an example of this –
script.py
from threading import Thread
def my_function():
# Code that takes time to run
Thread(target = my_function).start()
print("This is printed and the python interpreter returns control to MATLAB")
MATLAB Code
pyrunfile('script.py')
Here, the function 'my_function()' runs in the background and 'pyrunfile' returns immediately without blocking operations. This should allow your motor to move without freezing the preview.
Hope this helps.
  2 个评论
Paolo Neri
Paolo Neri 2023-3-31
Dear Suraj, thank you very much for your help! Following you ruggestion, I found the code in this link:
I implemented the script, and it seems to work fine in python, since the two functions work in parallel onseparate thread. When I run the same script through matlab, it looks like matlab still waits the script to complete before moving on.. In particular, on Matlab side I scripted:
tic
pyrunfile('Python\ProvaThread.py')
toc
Where ProvaThread.py is:
import threading
import time
'''
This function will print 5 lines in a loop and sleeps for 1 second after
printing a line.
'''
def threadFunc():
for i in range(5):
print('Hello from new Thread ')
time.sleep(1)
def main():
print('**** Create a Thread with a function without any arguments ****')
# Create a Thread with a function without any arguments
th = threading.Thread(target=threadFunc)
# Start the thread
th.start()
# Print some messages on console
for i in range(6):
print('Hi from Main Thread')
time.sleep(1)
# Wait for thread to finish
th.join()
if __name__ == '__main__':
main()
So the full time needed for the python script is the maximum between 5 and 6 (since the two functions has different duration but they work in parallel). I tried to change the ranges of the main function from 1 to 6. If I use 1, the total python time is about 5 seconds (because of threadFunc, since main funnction complete in 1 s), if I use 10 the total python time is about 10 second (because of main function). In this trials, the matlab tic-toc function provided again 5 s and 10 s, meaning that matlab still waited for the script to complete. In other words, it looks like the thread are in parallel in python, but matlab still waits for both of them to finish before moving on.
Is there any error in my coding?
Thank you very much
regards
Paolo
Paolo Neri
Paolo Neri 2023-3-31
Additionally, following your suggestion, I tried to "revert" the approach, and let matlab run the pyrun function on a background thread using parfeval. Unluckly, I got the error:
Error: Use of function pyrunfile is not supported on a thread-based worker.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by