0

I have 3 functions in my program audio(), motion() and vibration() and each of which is defined in such a way that they collect data from their respective sensors. I wrote a python program that collects data from audio and vibration sensor and a C program to get data from motion sensor (it was much faster than python).

Initially I was using 'Processes' from multiprocessing library to run audio() and vibration() simultaneously which worked absolutely fine. But now when I am trying run motion() along with them both, then the problem comes to existence. The issue is that the motion() process does not starts until the audio() is completed or vice versa and since it is supposed to be running on realtime, I need to give keyboard interrupt in order to finish audio() or motion() process.

From my understanding either process (motion and audio) waits for each other to complete and then starts execution. But my goal is to run them all together so that I can get data on the same timeline.

def vibration():
 #collects vibration data from raspberry shake
 ....
 ....
def audio():
 #uses pyaudio to record audio data
 ....
 ....
def motion():
 command = 'rtl_sdr -f 433000000 -g 15 -s 1024000 - | ./rf_receiver'
 subprocess.run(command)
p1 = Process(target=_thread.start_new_thread(fetch_data, ("hello",)))
p1.start()
p2 = Process(target=audio())
p2.start()
p3 = Process(target=motion())
p3.start()
p1.join()
p2.join()
p3.join()
asked Sep 13, 2019 at 0:36
2
  • Q: What platform is Python running on? Windows? A RaspberryPi? Something else? Q: Why do you think that p1, p2 and p3 aren't all running concurrently? Are you sure about that? Commented Sep 13, 2019 at 0:46
  • @paulsm4 The python program is running on an Ubuntu system and I am certain about them not running together because I am printing out the outputs of all the processes on the terminal window. I can see the output of P1 and P2 and the moment P2 is terminated, P3 starts executing. I tried to change the order by running P3 before P2 and I am getting the same results, i.e. P2 starts after P3 is terminated. Commented Sep 13, 2019 at 0:49

1 Answer 1

1

Your problem is that you're calling your worker functions from your main process and only passing their return values as the target parameter to Process. You probably want something like p2 = Process(target=audio) (without the () after audio), and p3 = Process(target=motion). You probably also want something similar for p1, but I have no idea what your current code is doing with threads there, so I can't fix it easily.

answered Sep 13, 2019 at 0:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.