How to apply multiprocessing in my Python code?
Question: I need help with applying multiprocessing in my code. I tried reading the
multiprocessing section of the Python documentation, but I just don't know how
to apply it to what I have. I believe what I do want to use is Pool, though.
Below is part of one of the python scripts I have written that will eventually
be called to another main script:

    
    
    ## servoRemote.py
    
    from franges import drange
    from cmath import sqrt as csqrt
    from math import atan, degrees, radians, tan, sqrt, floor
    import servo
    
    # Declare variables for servo conditional statements:
    UR = False
    UL = False
    BR = False
    BL = False
    
    def servoControl(x,y):
        global UR, UL, BR, BL
        ytop = round((-csqrt((0.3688**2)*(1-((x-0.5)**2)/(0.2067**2)))+0.5).real,3)
        ybottom = round((csqrt((0.3688**2)*(1-((x-0.5)**2)/(0.2067**2)))+0.5).real,3)
    
        if (x in list(drange(0.5,0.708,0.001,3)) and y in list(drange(ytop,0.501,0.001,3))):
            UR = True
            UL = False
            BR = False
            BL = False
            (factor, angle) = linearSF(x,y)
            (servo1, servo2) = angleSF(factor,angle)
            servo.move(1,servo1)
            servo.move(2,servo2)
    
    def linearSF(r,s):
        # Calculates the hypotenuse of the gaze:
        distr = abs(r-0.4999)
        dists = abs(0.50-s)
        theta = atan(dists/distr)
        b = sqrt(distr**2+dists**2)
    
        # Involved in solving for max x coordinate:
        A = 1+0.31412198*tan(theta)**2
        B = (-1-0.31412198*tan(theta)**2)**2 - 4*(1+(tan(theta)**2)/3.183477)*(0.207275+0.0785305*tan(theta)**2)
        B = csqrt(B)
        C = 2+((2*tan(theta)**2)/3.183477)
    
        # Different x equations:
        xRight = ((A+B)/C).real
        xLeft = ((A-B)/C).real
    
        if (UR == True and UL == False and BR == False and BL == False):
            x = xRight
            y = -sqrt((0.3688**2)*(1-((x-0.5)**2)/(0.2067**2)))+0.5
        # Solve for max hypotenuse given an angle, a:
        a = sqrt(abs(x-0.5)**2+abs(0.5-y)**2)
        # Final outputs, factor and angle (in degrees):
        factor = (b/float(a))
        angle = degrees(theta)
        return (factor, angle)
    
    def angleSF(factor, angle):
        # Angular factors:
        S1U = -0.0025641026*angle + 1.230759
        S2R = 0.0025641026*angle + 1
        if (UR == True and UL == False and BR == False and BL == False):
            servo1 = int(floor((S1U*65-78)*factor + 78))
            servo2 = int(floor((S2R*65-78)*factor + 78))
        return (servo1,servo2)
    

The code above is only for the case when UR == True. There are also other
conditional if statements that follow with different conditions. Most of the
examples I found using multiprocesses use a finite for loop, but I would like
to put this in a while like so:

    
    
    while 1:
        x = [some continuously incoming data stream]
        y = [some continuously incoming data stream]
        servoControl(x,y)
    

Thanks again in advance! I'm sure if I understand how to do it for this one
script, I will probably able to figure out how to apply it to the other ones.


Answer: Multiprocessing is a bit daunting. The key thing to understand is that you do
not want more than one process using a resource (for example, writing to a
reference) when other processes are using it (reading or writing).

So for your ideal multiprocessing setup, you want each process to work only on
resources that it alone is using.

So multi-processing in python, can be used effectively when 1) the multiple
processes work on different resources and 2) either you have multiple
processors on your hardware, or multiple processes can swap between each other
on a single (or multiple) processor machine while waiting for events to occur.

I would recommend getting your feet wet experimenting with "some toys" that
meet the conditions above to get a feel for how and where it works. Then
revisit your problem and try to implement it in a way consistent with how
python multiprocessing works.

Read the documentation thoroughly. There is join () function that you will
likely need to make sure that all of your processes finish, before whatever
the next step might be, even if the next step is just to terminate the
program.

