0

I run a script that executes multiple processes, that are executed in different classes. The processes do not end unless the user asks for it. So in the main class I decided to create a process that will exit() the script, I supposed this will kill all the processes too. This is the main.py class.

def finishTest():
    end = "notexit"
    while end != "exit":
        end = input("To finish the test type 'exit'")
    exit()


if __name__ == '__main__':

    fT = Process (target=finishTest)
    fT.start()
    #this are functions of this class that call functions from other classes that run the other processes.
    askTest()
    execTest()

When I execute the main.py the following error appears,

Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "execution.py", line 20, in finishTest
    exit = input("To finish the test type 'exit'")
EOFError: EOF when reading a line

How do I correct this error? Isn't it this a correct way to stop a script and all the processes being executed by the script?

Thank you all!

Nkolot
  • 87
  • 2
  • 9
  • First of all you should think about renaming your exit variable (as `exit` is a pre-existing function), second, I don't think the issue is in the function you shared here (`finishTest()`) - just tried running it alone and aside from an error on the exit function it works fine – Jessica Chambers Mar 14 '19 at 09:26
  • @DirtyBit if the user types "exit" in the prompt, then `exit` will be set to "exit" – Jessica Chambers Mar 14 '19 at 09:27
  • @JessicaChambers Nope, it would throw an error: `exit() TypeError: 'str' object is not callable` – DirtyBit Mar 14 '19 at 09:28
  • Because the variable name is the same as the function name, hence my comment about renaming the variable. In either case I don't recieve the `EOF` error – Jessica Chambers Mar 14 '19 at 09:29
  • with some tweaks I have this which runs error free: ``` my_exit = "notexit" while my_exit != "exit": my_exit = input("To finish the test type 'exit'") print("bye bye!") ``` – Jessica Chambers Mar 14 '19 at 09:29
  • I rename the variable to "end". Doesn't work executing it in shell neither in Pycharm. – Nkolot Mar 14 '19 at 09:30
  • @Nkolot do you still have the `EOF` error? – Jessica Chambers Mar 14 '19 at 09:30
  • yes I do, File "execution.py", line 20, in finishTest end = input("To finish the test type 'exit'") EOFError: EOF when reading a line – Nkolot Mar 14 '19 at 09:31
  • is the error thrown before or after you have typed something? Before or after you type "exit"? – Jessica Chambers Mar 14 '19 at 09:34

1 Answers1

0

Change the variable exit name and try putting it in a try-except block:

def finishTest():
    ex = "notexit"
    while ex != "exit":
        try:
            ex = input("To finish the test type 'exit'")
        except EOFError:
            pass
    exit()

OUTPUT:

To finish the test type 'exit'hey
To finish the test type 'exit'okay
To finish the test type 'exit'bye
To finish the test type 'exit'exit

Process finished with exit code 0
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • This corrects the EOFError but logically executes an infinite loop. I get out the print message out of the while loop, it seems that works fine but the script doesn't stop. – Nkolot Mar 14 '19 at 09:39
  • @Nkolot It does not, added a sample output. – DirtyBit Mar 14 '19 at 09:41
  • I get out the print message out of the while loop, it seems that works fine but the script doesn't stop. – Nkolot Mar 14 '19 at 09:43
  • Which script does not stop? What do you get? – DirtyBit Mar 14 '19 at 09:52
  • The main.py script, the one and only I'm executing...I have processes inside but just one script. – Nkolot Mar 14 '19 at 09:58
  • @Nkolot That is expected behaviour, think of it as `n` number of threads executing and calling the function `finishTest()`. – DirtyBit Mar 14 '19 at 10:13
  • @Nkolot read here: https://stackoverflow.com/questions/17564804/how-to-wait-until-only-the-first-thread-is-finished-in-python – DirtyBit Mar 14 '19 at 10:18
  • 1
    Thank you for your help, I will look it and stake out the way to finish the processes. – Nkolot Mar 14 '19 at 10:23