How to connect 2 ultrasonic sensors concurrently using python multiprocessing process?
<p>I'm new to python but i'm working on a project in which i need to connect 3 or more Ultrasonic Sensors concurrently. I read all about threads and multiprocessing ran a couple of examples successfully. I know the code has to be run form the command prompt or the PI2 terminal. However, the multiprocessing code I wrote does not work and I cannot figure out why. Could someone please help me.  </p>

<pre><code> from multiprocessing import Process
 from gpiozero import DistanceSensor

 ultrasonic = DistanceSensor(echo=18, trigger=23)
 ultrasonic_B = DistanceSensor(echo=25, trigger=24)
 ultrasonic_C = DistanceSensor(echo=12, trigger=16)

 ultrasonic.max_distance = 1
 ultrasonic_B.max_distance = 1
 ultrasonic_C.max_distance = 1

 def A(name):
    while True:
        ultrasonic.wait_for_in_range()
        print('Distance')

        if ultrasonic.wait_for_out_of_range():
            print('Out of range')

def B(name):
    while True:
        ultrasonic_B.wait_for_in_range()
        print('Distance_B')

        if ultrasonic_B.wait_for_out_in_range():
            print('Out of range_B')

def C(name):
    while True:
        ultrasonic_C.wait_for_in_range()
        print('Distance_B')

        if ultrasonic_C.wait_for_out_in_range():
            print('Out of range_B')

if __name__ == "__main__":

    p = Process(target=A)
    p1 = Process(target=B)
    p2 = Process(target=C)

    p.start()
    p1.start()
    p2.start()
</code></pre>

<p>I took your suggestions into consideration and was able to make the first loop work but the other 2 loops give me nothing. Here is the updated code  </p>

<pre><code>from multiprocessing import Process
from gpiozero import DistanceSensor

ultrasonic = DistanceSensor(echo=18, trigger=23)
ultrasonic_B = DistanceSensor(echo=25, trigger=24)
ultrasonic_C = DistanceSensor(echo=12, trigger=16)

ultrasonic.max_distance = 1
ultrasonic_B.max_distance = 1
ultrasonic_C.max_distance = 1

def A():
    while ultrasonic.wait_for_in_range():
        print('Distance')

        if ultrasonic.wait_for_out_of_range():
            print('Out of range')

def B():
    while ultrasonic_B.wait_for_in_range():
        print('Distance_B')

        if ultrasonic_B.wait_for_out_in_range():
            print('Out of range_B')

def C():
    while ultrasonic_C.wait_for_in_range():
        print('Distance_B')

        if ultrasonic_C.wait_for_out_in_range():
            print('Out of range_B')

if __name__ == "__main__":

    p = Process(target=A)
    p1 = Process(target=B)
    p2 = Process(target=C)

    p.run()
    p1.run()
    p2.run()
</code></pre>

<p>Result comes form first loop
    Distance
    Out of range
    Distance
    Out of range</p>

<p>OK this is what I have</p>

<pre><code>from multiprocessing import Process
from gpiozero import DistanceSensor

ultrasonic = DistanceSensor(echo=18, trigger=23)
ultrasonic_B = DistanceSensor(echo=25, trigger=24)
ultrasonic_C = DistanceSensor(echo=12, trigger=16)

ultrasonic.max_distance = 1
ultrasonic_B.max_distance = 1
ultrasonic_C.max_distance = 1

def A():
    while ultrasonic.wait_for_in_range():
        print('Distance')

        if ultrasonic.wait_for_out_of_range():
            print('Out of range')

def B():
    while ultrasonic_B.wait_for_in_range():
        print('Distance_B')

        if ultrasonic_B.wait_for_out_in_range():
            print('Out of range_B')

def C():
    while ultrasonic_C.wait_for_in_range():
        print('Distance_B')

        if ultrasonic_C.wait_for_out_in_range():
            print('Out of range_B')

if __name__ == "__main__":

    p = Process(target=A)
    p1 = Process(target=B)
    p2 = Process(target=C)

    p.start()
    p1.start()
    p2.start()
</code></pre>

<p>You do not say what you mean with "does not work", so I am taking a few guesses here.</p>

<p>The obvious fail here would be:</p>

<blockquote>
  <p>TypeError: A() takes exactly 1 argument (0 given)</p>
</blockquote>

<p>Since functions <code>A</code>, <code>B</code> and <code>C</code> all take an argument <code>name</code>, and you do not provide it in <code>Process(target=A)</code>. It works if you just remove the parameter from the functions, since you are not even using it.</p>

<p>You can also provide the argument in the call like this:</p>

<pre><code>p = Process(target=A, args=('ultra_a',))
</code></pre>

<p>Other one could be indentation error, at least in your code paste you have one extra space at each line until def B.</p>

python