Python Forum
Wait for command within a process
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wait for command within a process
I'm attempting to set up a process which listens for commands. Eventually this will be implemented with TCP/UDP and MODBUS, but for now I am trying to get it to work from within a process. The syntax is simple: I am starting a process with the target being the listenForCommand() function. This function is supposed to wait for input from the user using the input() function.
The program does not, however, wait for input. It instead throws an EOFError: EOF when reading a line. However, if I run the listenForCommand() function by itself, without it being executed from a process, then it works fine. Somehow the process is not allowing input() to wait for input. Here's some code, and output:

from multiprocessing import Process

def listenForCommand():
    # Capture user input
    print("-1:Exit 1:Home 2:LoadCard:")
    selection = int(input())
    # Execute selected action
    while selection != -1:
        if(selection == 1):
            p1 = Process(target=Routines.home, args=(VacuumTrayTravel,Main_Registry))
        elif(selection == 2):
            p1 = Process(target=Routines.loadCard)
        # Prompt user again
        print("-1:Exit 1:Home 2:LoadCard:")
        selection = int(input())

# Main execution starts here
-1:Exit 1:Home 2:LoadCard: Process Process-1: Traceback (most recent call last): File "/usr/lib/python3.5/multiprocessing/", line 249, in _bootstrap File "/usr/lib/python3.5/multiprocessing/", line 93, in run self._target(*self._args, **self._kwargs) File "/home/debian/RMI/src/", line 158, in listenForCommand selection = int(input()) EOFError: EOF when reading a line
OR, this is how I am running it standalone:
# Main execution starts here
-1:Exit 1:Home 2:LoadCard: 1 [Code executes] -1:Exit 1:Home 2:LoadCard
multiprocessing disables the stdin when it starts a process, therefore input() cannot be used.
Quote:The multiprocessing module doesn't let you read stdin. That makes sense generally because mixing stdin readers from multiple children is a risky business. In fact, digging into the implementation, multiprocessing/ explicitly sets stdin to devnull:

sys.stdin = open(os.devnull)

I've instead swapped to using a Thread to listen for the input:
from threading import Thread

# Main execution starts here

Possibly Related Threads…
Thread Author Replies Views Last Post
  Run an app in a standalone terminal and wait until it's closed glestwid 2 825 Aug-30-2020, 08:14 AM
Last Post: glestwid
  python os.popen is not working for wait method elenaflorence87 0 576 Jul-22-2020, 12:56 PM
Last Post: elenaflorence87
  pydev debugger: process 3442 is connecting when I run a program with ALT+COMMAND+R Seneca260 1 1,205 Jan-06-2020, 06:57 PM
Last Post: micseydel
  How to sharing object between multiple process from main process using Pipe Subrata 1 1,842 Sep-03-2019, 09:49 PM
Last Post: woooee
  changing the process command line arguments in python Skaperen 3 1,354 Aug-19-2019, 02:40 PM
Last Post: wavic
  Subprocess.send_signal, wait until completion plinio 5 5,727 Jun-29-2018, 12:07 PM
Last Post: plinio
  How do you wait for a daemon to reach a certain state with subprocess.popen()? phalse 3 4,809 Jul-08-2017, 12:32 PM
Last Post: DeaD_EyE
  just a trial. Please wait 5 minutes before deleting this thread sylas 2 1,901 Jun-06-2017, 03:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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