Why won't functions run in parallel?
<p>I've been reading up and trying to implement multithreading into my program, but no matter how I do it, it will not run my functions in parallel. I'm using sensors for a raspberry pi 3, trying to have them print out statuses in parallel rather than wait for one to finish and then move to the next function.</p>

<p>What happens now is that it waits for those 20 seconds before the program checks the seconds sensor and prints out that status message. I have no idea why!</p>

<p>Code:</p>

<pre><code>import RPi.GPIO as GPIO
import time
from multiprocessing import Process

''' Define pins and setup the sensors '''

def runInParallel(*fns):
    proc = []
    for fn in fns:
        p = Process(target=fn)
        p.start()
        proc.append(p)
    for p in proc:
        p.join()

def sensor1():
    #Sleep timer long so I can check that I can see prints from 2nd sensor while this thread is sleeping
    time.sleep(20)
#Get status from sensor---
    if status == 1:
        print "Ouch!"
    else:
        print "Good!"

def sensor2():
    time.sleep(0.2)
#Get status from 2nd sensor---
    if status == 1:
        print "Ouch2!"
    else:
        print "Good2!"

runInParallel(sensor1, sensor2)
</code></pre>

<p>I don't know why your example isn't working, but I tried this:</p>

<pre><code>    import time
    from threading import Thread

    ''' Define pins and setup the sensors '''
    status = 0

    def runInParallel(*fns):
        proc = []
        for fn in fns:
            p = Thread(target=fn)
            proc.append(p)
        for p in proc:
            p.start()

    def sensor1():
        #Sleep timer long so I can check that I can see prints from 2nd sensor while this thread is sleeping
        time.sleep(.2)
    #Get status from sensor---
        if status == 1:
            print("Ouch!")
        else:
            print("Good!")



    def sensor2():
        time.sleep(0.2)
    #Get status from 2nd sensor---
        if status == 1:
            print("Ouch2!")
        else:
            print("Good2!")

    runInParallel(sensor1, sensor2)
</code></pre>

<p>and it outputted <code>good2</code> and <code>good</code> almost simultaneously. If you really need the outputs to be exact then try to debug you example, but if closer than you can notice with the naked eye is OK then I think that the threading module will work well enough.</p>

<h2>EDIT:</h2>

<p>Ok I think that you problem is that you think that the <code>Process.join()</code> counts the waits in the function.  <code>Process.join()</code> only makes sure that the functions <em>start</em> at the same time. If you have a wait in one function, then <code>runInParallel</code> won't care about that.</p>

python linux raspberry-pi gpio