123321123321
123321123321

Reputation: 79

Python multiprocessing more infinite loops at the same time

EDIT: I SOLVED IT! DON'T MAKE THE SAME MISTAKE AS ME

replace this line:

p = multiprocessing.Process(target=check(mval,mname))

with:

p = multiprocessing.Process(target=check, args=(mval,mname))

---------------------------------------------------------------------------------------

.

.

.

I am making a robot with Raspberry Pi and some microswitches and I want to check the microswitches if they are triggered so I'm using the multiprocessing module, the process class to be exact with a few infinite while loops and the problem is that one starts and waits until it is triggered and ends and starts the next one instead of all of them starting and running independently. Here is my code so far.

import multiprocessing
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(32, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(7, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(36, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def check(mval, mname):
    while True:
        time.sleep(0.01)
        check = GPIO.input(mval)
        if check == False:  
            print("triggered "  + mname)
            with open("tmp.txt", "w"):
                pass
            f = open("tmp.txt", "w")
            f.write(mname)
            f.close()
            break

def startcheck(mval, mname):
    p = multiprocessing.Process(target=check(mval,mname))
    p.start()
    p.join()


startcheck(32, "m1")
startcheck(7, "m2")
startcheck(12, "m3")
startcheck(36, "m4")

Upvotes: 1

Views: 2261

Answers (1)

Paul Cornelius
Paul Cornelius

Reputation: 10916

The join() function causes each loop to terminate before the next one starts. From the standard library docs:

"join([timeout]) If the optional argument timeout is None (the default), the method blocks until the process whose join() method is called terminates. If timeout is a positive number, it blocks at most timeout seconds.

A process can be joined many times.

A process cannot join itself because this would cause a deadlock. It is an error to attempt to join a process before it has been started."

Solution: remove the join() line.

Upvotes: 1

Related Questions