1

I am doing a project where I am required to measure vibration levels due to passing vehicles (using an accelerometer sampling at high rate) and capture video of the event "SIMULTANEOUSLY" when the vibrations exceed a certain threshold value. I want both the processes i.e. data acquisition and video recording to go simultaneously so that they are synchronized in time.

I am doing both the things on a raspberry pi. To achieve my goal I have 2 strategies in mind.

1) Write a function for

a) Data acquisition
b) Recording video

and run them in the same script.

2) Or while running the script for data acquisition, use a call to os.system, to run a script to capture video of the event.

Now I am curious in what order are functions executed in Python? So if I write

while 1:

   if v_threshold > some_value:

      Acquire_data(); Capture_Video(); 

Are they being executed simultaneously or does Capture_Video() only begin once Acquire_data() has finished execution? Or in general, how are functions executed in Python (or any other programming language).

What would be your advice to achieve the goal? Writing functions or running 2 parallel scripts? Or should I use the multiprocessing module?

Thanks

Nischal

2
  • They are executed sequentially, one after the other. Try making a dummy function for each that sleeps 5 seconds and you'll see it takes 10 seconds. You will need to use multiprocessing or multithreading to get both to run at once. Commented Dec 21, 2017 at 9:05
  • Thanks, Mark I get the idea. Commented Dec 21, 2017 at 11:25

1 Answer 1

1

I would go for option 1 as you have more control over the whole process (e.g. you can easily wait until both subprocesses are done)

Your main setup would look like this:

from multiprocessing import Process
import time

def acquire_data(arg):
    for i in range(5):
        print('acquiring data: {}'.format(arg))
        time.sleep(1.1)

def capture_video():
    for i in range(5):
        print('capturing video')
        time.sleep(1)


if __name__ == '__main__':
    p_data = Process(target=acquire_data, args=('foo',))
    p_video = Process(target=capture_video)
    p_data.start()
    p_video.start()
    p_data.join() # wait until acquire_data is done
    p_video.join() # wait also until capture_video is done

Also: Which model is it? Because if both data-acquiring and video-capturing is taking 100% cpu then you will run into an issue with only a single core raspberry pi. Model 3 has 4 cores so this is fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thanks for your advice. It is Pi3 model B

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.