Python Forum
Parallel Processing in Python with Robot
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parallel Processing in Python with Robot
#1
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" 
Reply
#2
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
Reply
#3
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.
Reply
#4
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?
Reply
#5
don't know about woooee's answer, but that certainly can be done with asyncio
you can also just use plain threading
Reply
#6
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 
Reply
#7
Note that the 'multiprocessing' module will only work for antique python.
Bad information
It's now built in.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 704 Jun-22-2023, 02:33 AM
Last Post: woooee
  parallel loop python caro 4 1,738 Jun-16-2022, 08:46 PM
Last Post: woooee
  The Wall-E Robot Argentum 1 1,383 Apr-03-2022, 03:01 PM
Last Post: sastonrobert
  mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python ilknurg 3 5,465 Jan-18-2022, 06:25 PM
Last Post: ilknurg
  SimpleHTTPRequestHandler ( verses ) Files Python Processing JohnnyCoffee 0 1,750 Apr-29-2021, 02:47 AM
Last Post: JohnnyCoffee
  Real Time Audio Processing with Python Sound-Device not working Slartybartfast 2 3,882 Mar-14-2021, 07:20 PM
Last Post: Slartybartfast
  DarkPaw Robot code Tyrelex78 3 2,091 Nov-27-2020, 12:06 AM
Last Post: Tyrelex78
  Image Processing in Python JoeDainton123 1 17,102 Oct-05-2020, 12:08 AM
Last Post: Larz60+
  Matlab to Python -- Parallel Computing zistambo 1 1,934 Jun-10-2020, 04:59 PM
Last Post: pyzyx3qwerty
  Use dynamic variable from parallel running python script Sicksym 0 1,823 May-15-2020, 02:52 PM
Last Post: Sicksym

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020