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
#1
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)
        
        p1.start()
        p1.join()
        
        # Prompt user again
        print("-1:Exit 1:Home 2:LoadCard:")
        selection = int(input())

# Main execution starts here
Process(target=listenForCommand).start()
Output:
-1:Exit 1:Home 2:LoadCard: Process Process-1: Traceback (most recent call last): File "/usr/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap self.run() File "/usr/lib/python3.5/multiprocessing/process.py", line 93, in run self._target(*self._args, **self._kwargs) File "/home/debian/RMI/src/Main.py", 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
listenForCommand()
Output:
-1:Exit 1:Home 2:LoadCard: 1 [Code executes] -1:Exit 1:Home 2:LoadCard
Reply
#2
Solution:
multiprocessing disables the stdin when it starts a process, therefore input() cannot be used.

https://stackoverflow.com/questions/4283...ing-a-line
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/process.py explicitly sets stdin to devnull:

sys.stdin.close()
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
Thread(target=listenForCommand).start()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 6,223 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  wait for the first of these events Skaperen 4 1,872 Mar-07-2022, 08:46 PM
Last Post: Gribouillis
  how to do a two-way wait Skaperen 2 1,229 Mar-04-2022, 02:31 AM
Last Post: Skaperen
  Run an app in a standalone terminal and wait until it's closed glestwid 2 2,447 Aug-30-2020, 08:14 AM
Last Post: glestwid
  python os.popen is not working for wait method elenaflorence87 0 1,969 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 2,607 Jan-06-2020, 06:57 PM
Last Post: micseydel
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,619 Sep-03-2019, 09:49 PM
Last Post: woooee
  changing the process command line arguments in python Skaperen 3 2,946 Aug-19-2019, 02:40 PM
Last Post: wavic
  Subprocess.send_signal, wait until completion plinio 5 8,913 Jun-29-2018, 12:07 PM
Last Post: plinio

Forum Jump:

User Panel Messages

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