So I am attempting to write a piece of code that allows python to control GPIO pins on a Raspberry Pi using a timer that runs concurrently with a main program.
Here is a very simplified view of the code I am writing:
from multiprocessing import Process, Lock
from time import sleep
def motor_up(time, lock):
lock.acquire()
print("turn up PINS on")
sleep(time)
print("turn up PINS off")
lock.release()
def motor_down(time, lock):
lock.acquire()
print("turn down PINS on")
sleep(time)
print("turn down PINS off")
lock.release()
if __name__ == '__main__':
motor_lock = Lock()
time = 10
up = Process(target=motor_up, args = (time, motor_lock))
down = Process(target=motor_down, args = (time, motor_lock))
up.start()
sleep(4)
down.start()
essentially the locks are supposed to stop the other process from running until the first one has finished, which means that this process should take at minimum 20 seconds to run.
But when I run it a few weird things happen:
- it does not print "turn up PINS on" and then wait 10 seconds to print "turn up PINS off". it basically waits approximately 10 seconds, then prints both at the same time.
- it then waits about 10 seconds, and does the print statements for the other process in a similar fashion
turn up PINS on
turn up PINS off
turn down PINS on
turn down PINS off
[Finished in 20.151s]
I have no clue why this is happening, or how to fix it. I am very new to multiprocessing.
Any and all device is appreciated