停止脚本,EOFError:读取行时出现EOF

时间:2019-03-14 09:21:01

标签: python python-3.x bash process multiprocessing

我运行一个脚本,该脚本执行多个进程,这些进程在不同的类中执行。除非用户要求,否则过程不会结束。因此,在主类中,我决定创建一个将退出exit()脚本的进程,我认为这也会杀死所有进程。这是main.py类。

foreach($data as $key => $value) { 
    $json_arr = json_decode($data[0]['attendance_data'], true);
    $arr_index = array();
     foreach ($json_arr as $key1 => $value1) { 
        if ($value1['id'] != $childid) { 
            $arr_index[] = $key1; 
        } 
     } 
     foreach ($arr_index as $i) { 
        unset($json_arr[$i]); 
     } 
     $json_arr = array_values($json_arr);
     $json_arr_en = json_encode($json_arr);
     $data[$key1]['attendance_data'] = $json_arr_en; 
}

当我执行main.py时,出现以下错误,

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()

如何纠正此错误?这不是停止脚本以及该脚本正在执行的所有进程的正确方法吗?

谢谢大家!

1 个答案:

答案 0 :(得分:0)

更改变量exit的名称,然后尝试将其放在try-except块中:

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

输出

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