使用 Process 一次激活多个 while 循环时如何停止出现错误?

How do I stop getting errors when using Process to activate multiple while loops at once?

提问人:Shearedsheep 提问时间:9/30/2023 更新时间:9/30/2023 访问量:47

问:

我是 Python(以及一般编程)的初学者,作为对自己的一个小测试,我做了一个程序,它有点像手机上的锁屏。随着时间的推移,我仍然需要 strike 来回落,并寻找一些同时运行多个 while 循环的方法。我决定使用 Process,但现在每次尝试运行程序时都会出现错误。

程序:

import time
from multiprocessing import Process
Password = 20051402  # password of your choice
strike = 0  # defines strike
password = None  # defines password "at the module level"


def loop_redemption():  # decreases strike over time
    global strike
    while True:
        while strike >= 1:
            time.sleep(5)
            strike -= 1


def loop_main():
    global strike
    while True:
        if strike >= 3:  # pauses for 5 seconds while counting down
            for time_passed in range(5):
                print("Timed-out for " + str(5 - time_passed) + " seconds.")
                time.sleep(1)

        global password
        password = int(input("Password: "))
        if password == Password:
            Process(target=loop_redemption).terminate()
            break  # ends loop when correct password is entered
        else:
            strike += 1  # gives you a strike everytime you get the password wrong

        if strike == 1:
            print("Two attempts remaining before time-out.")
        if strike == 2:
            print("One attempt remaining before time-out.")


if __name__ == '__main__':  # allows both while-loops to run simultaneously?
    Process(target=loop_redemption).start()
    Process(target=loop_main).start()

if password == Password:
    print("Password correct. Enjoy your day.")

跑:

Password: Process Process-2:
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 314, in _bootstrap
    self.run()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\vdtro\AppData\Roaming\JetBrains\PyCharmCE2023.2\scratches\Password.py", line 25, in loop_main
    password = int(input("Password: "))
                   ^^^^^^^^^^^^^^^^^^^
EOFError: EOF when reading a line

这是我使用多处理之前的样子,因此它的结果更接近它们应该的样子。(此版本中无法下行):

import time
Password = 20051402  # password of your choice
strike = 0  # defines strike

while True:
    if strike >= 3:  # pauses for 5 seconds while counting down
        for time_passed in range(5):
            print("Timed-out for " + str(5 - time_passed) + " seconds.")
            time.sleep(1)

    password = int(input("Password: "))
    if password == Password:
        break  # ends loop when correct password is entered
    else:
        strike += 1  # gives you a strike everytime you get the password wrong

    if strike == 1:
        print("Two attempts remaining before time-out.")
    if strike == 2:
        print("One attempt remaining before time-out.")

print("Password correct. Enjoy your day.")
python-3.x 多处理 eof self

评论


答: 暂无答案