Gatsby25
Posts: 26
Joined: Wed Jun 09, 2021 10:21 pm

Is it possible to run Python functions simultaneously?

Fri Jun 18, 2021 3:14 am

Hello. Let's say we have the following in Python:
  1. Function 1: Turns on LED
  2. Function 2: Turns on buzzer
  3. Function 3: Turns on motor
Would it be possible to wrap all three functions into one function and have them run at the same time?

Anybody have experience with this? If so, what library did you use?

EDIT: I should have added more details initially, but my idea was to wait for a motion sensor to be activated. Once activated, this should trigger a function that would turn on the following at the same time: a Pi Camera, a stepper motor, some LED strips, and then a buzzer / speaker. Basically an alarm system that should start these devices all at once. The sequence would be: when triggered, the Pi Camera would start recording, the stepper motor would move something until it reaches a certain distance, then the LED strip and buzzer/speaker would turn on lights and play a sound at the same time.
Last edited by Gatsby25 on Fri Jun 18, 2021 7:28 pm, edited 1 time in total.

gordon77
Posts: 5774
Joined: Sun Aug 05, 2012 3:12 pm

Re: Is it possible to run Python functions simultaneously?

Fri Jun 18, 2021 6:08 am

Do you have any existing code you can show ?

Are the 3 items connected to 3 gpio outputs ?

Using one script to control three items shouldn't be a problem.

dbrion06
Posts: 511
Joined: Tue May 28, 2019 11:57 am

Re: Is it possible to run Python functions simultaneously?

Fri Jun 18, 2021 1:13 pm

turning a GPIO on takes less than 1E-6 microseconds (I neglected python overhaed).
What is your definition of "simultaneously"

pfletch101
Posts: 661
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Is it possible to run Python functions simultaneously?

Fri Jun 18, 2021 1:29 pm

Gatsby25 wrote:
Fri Jun 18, 2021 3:14 am
Hello. Let's say we have the following in Python:
  1. Function 1: Turns on LED
  2. Function 2: Turns on buzzer
  3. Function 3: Turns on motor
Would it be possible to wrap all three functions into one function and have them run at the same time?

Anybody have experience with this? If so, what library did you use?
Depending on the details of what you are trying to do, look at the Python documentation of threading and/or multiprocessing. Threading is (IMHO) easier to conceptualize and manage, but has some limitations, particularly if more than one thread is doing a lot of computations (as opposed to I/O or sleeping). Multiprocessing imposes almost no limitations on the individual processes, but synchronization and communication between them are more complex.

User avatar
B.Goode
Posts: 12270
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Is it possible to run Python functions simultaneously?

Fri Jun 18, 2021 2:10 pm

Gatsby25 wrote:
Fri Jun 18, 2021 3:14 am
Hello. Let's say we have the following in Python:
  1. Function 1: Turns on LED
  2. Function 2: Turns on buzzer
  3. Function 3: Turns on motor
Would it be possible to wrap all three functions into one function and have them run at the same time?

Anybody have experience with this? If so, what library did you use?

Are there any realistic limits on what 'simultaneously'/'at the same time' means?

Each operation is pretty-much 'instantaneous', so would it suffice to perform the 3 actions in an unbroken sequence? Would a human user perceive the difference?

Gatsby25
Posts: 26
Joined: Wed Jun 09, 2021 10:21 pm

Re: Is it possible to run Python functions simultaneously?

Fri Jun 18, 2021 7:27 pm

B.Goode wrote:
Fri Jun 18, 2021 2:10 pm
Gatsby25 wrote:
Fri Jun 18, 2021 3:14 am
Hello. Let's say we have the following in Python:
  1. Function 1: Turns on LED
  2. Function 2: Turns on buzzer
  3. Function 3: Turns on motor
Would it be possible to wrap all three functions into one function and have them run at the same time?

Anybody have experience with this? If so, what library did you use?

Are there any realistic limits on what 'simultaneously'/'at the same time' means?

Each operation is pretty-much 'instantaneous', so would it suffice to perform the 3 actions in an unbroken sequence? Would a human user perceive the difference?
I should have added more details in my original post, but my idea was to wait for a motion sensor to be activated. Once activated, this should trigger a function that would turn on the following at the same time: a Pi Camera, a stepper motor, some LED strips, and then a buzzer / speaker. Basically an alarm system that should start these devices all at once. The sequence would be: when triggered, the Pi Camera would start recording, the stepper motor would move something until it reaches a certain distance, then the LED strip and buzzer/speaker would turn on lights and play a sound at the same time.

I will read more about multiprocessing and see if that's appropriate in this case.

Zenot1c
Posts: 2
Joined: Fri Jun 18, 2021 6:59 pm

Re: Is it possible to run Python functions simultaneously?

Fri Jun 18, 2021 7:39 pm

Yes, there is a module called Async I believe, though for your use case, i would say that code runs so quick, even on low powered machines like Pis, that there is no need. Between each function running there would most likely be less than 0.1 seconds delay.

Return to “Beginners”