Python Forum

Full Version: Parallel Processing in Python with Robot
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have coded a robot that runs modules containing the commands necessary to carry out a specific process. As of now, the user inputs the desired command, my code uses an if statement to determine what command they entered, and it runs the appropriate command. However, the command must be finished before the user can input another command.

Now I would like to do the following: have the user input a command, start the command, and, while the command is running, rerun the command to get the users input. For example, the user inputs move forward, the robot starts moving, then the user changes the command to move backward midway through the robot moving forward, and the robot resets and starts moving backward in response.

Below is the code of the while loop that runs the modules and asks for user input. Let me know if you have any ideas on how I can achieve this or if you need any clarification. I am a high schooler who is still learning how to code, so any help would be greatly appreciated.

Thanks in advance.

Best,

Christopher

#runs all the modules and gets user input
while True: 
    defultPosition()
    command = raw_input("Enter move forward, move backward, turn or cancel: ")
    defultPosition()
    if command == "cancel":
        break 
    if command == ("move forward") or (command == "move backward"):
        speedInput = input("Enter the desired speed: ")
        distanceInput = input("Enter the number of inches you wish the robot to move (must be a factor of 5): ")
    if command == "turn":
        speedInput = input("Enter the desired speed: ")
        degrees = input("Enter the number of degrees for the robot to move: ")

    print ("\nINPUTED COMMAND: %s \n" % command)

    if command == "move forward":
        #run the moveForward module

        print "Initiating command\n"

        moveForward(speedInput, distanceInput)

        print "Finished command; restarting and waiting for another input \n"

    if command == "move backward":
        #run the moveBackward module

        print "Initiating command\n"

        moveBackward(speedInput, distanceInput)

        print "Finished command; restarting and waiting for another input \n"

    if command == "turn":
        #runs the turn module

        print "Initiating command\n"

        turnCounterClockwise(speedInput, degrees)

        print "Finished command; restarting and waiting for another input \n" 
This looks to be a pretty good write-up on asynchronous processing: https://medium.freecodecamp.org/a-guide-...2e2afa44f6
There are lots of others, including one here: https://python-forum.io/Thread-Exploring...ht=asyncio
Because you want to do 2 things at once, you would use a multiprocessing Process to run the robot code. Input can then be done separately from the command line. To communicate with a running process, use a multiprocessing Manager dictionary or list, i.e. update the Manager dict or list with the input from the command line. The robot process would check for a change each pass through the while.
Would the method described by “woooee” work even if I want to stop or change the command halfway through the carrying out of the move forward module?
If not, how would I do this?
don't know about woooee's answer, but that certainly can be done with asyncio
you can also just use plain threading
A simple program (with messy output) to start a counter (robot moving) while getting info from the keyboard. See Doug Hellmann's Python Module of the Week, "Managing Shared State" at https://pymotw.com/3/multiprocessing/communication.html You create a Manager Dictionary, pass it to the process per the example at PyMOTW, and then use it like a regular dictionary from within or outside of the process.
import time
from multiprocessing import Process

def another_process(spaces):
        ctr=1

        print("spaces", len(spaces))
        while True:
            print(spaces, ctr)
            ctr += 1
            time.sleep(1)

if __name__ == "__main__":
    spaces=""
    ## start with one running process
    processes_list=[]
    p=Process(target=another_process, args=(spaces,))
    p.start()
    processes_list.append(p)

    while True:
      option=input('''"s" to start another process
"q" to quit this program ''')
      option=option.lower()
      if "s" == option:
        spaces += "     "
        p=Process(target=another_process, args=(spaces,))
        p.start()
        processes_list.append(p)
      elif "q" == option:
        for p in processes_list:
            p.terminate()
            p.join()
        break 
Note that the 'multiprocessing' module will only work for antique python.
Bad information
It's now built in.