在 Python 中进行多处理时无法使用输入

Unable to use input when multiprocessing in Python

我想运行同时处理2个进程。 1 将保持每秒打印 'a',另一个将请求输入,当输入为 'Y' 时,第一个进程将停止打印 'a'。我是 Python 的新手,我搞不懂...

这是我到目前为止想出的:

from multiprocessing import Process
import time

go = True

def loop_a():
    global go

    while go == True:
        time.sleep(1)
        print("a")

def loop_b():
    global go
    text = input('Y/N?')

    if text == 'Y':
        go = False

if __name__ == '__main__':
    Process(target=loop_a).start()
    Process(target=loop_b).start()

这是我收到的错误消息:

Process Process-2:
Traceback (most recent call last):
  File "C:\Users\Tip\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 249, in _bootstrap
    self.run()
  File "C:\Users\Tip\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "F:\ProgrammingTK\PROGproject\test.py", line 15, in loop_b
    text = input('Y/N?')
EOFError: EOF when reading a line

扩展 jasonharper 的评论,因为他是正确的。

有几个问题

  1. go 变量 在进程之间共享。正如 Jason 所建议的那样,您可以在 multiprocessing 中使用类似 Manager 的内容,以便在多个进程之间共享一个值。从技术上讲,go 变量将被复制到每个进程中,但不会在它们之间共享,因此一个进程中的更改不会被另一个进程看到。
  2. 同样,正如他提到的,您需要将 input(..) 拉入程序的主线程。此外,如果您使用的是 2.7,则需要使用 raw_input(..).
  3. 此外,如果您只检查一次标志然后退出,那么您可能会遇到 BrokenPipeError

考虑到这一点,您可以尝试这样的操作:

from multiprocessing import Process, Manager
import time


def loop_a(go):
    while True:
        # run forever and print out the msg if the flag is set
        time.sleep(1)
        if go.value:
            print("a")

if __name__ == '__main__':
    # shared value flag
    manager = Manager()
    go_flag = manager.Value('flag', True)

    # other process that is printing
    Process(target=loop_a, args=(go_flag,)).start()

    # normal main thread; toggle on and off the other process
    while True:
        text = input('Stop Y/N?')
        if text == 'Y':
            go_flag.value = False
            print("Changed the flag {}".format(go_flag.value))
        else:
            go_flag.value = True
            print("Changed the flag {}".format(go_flag.value))