在 Python 中进行多处理时无法使用输入 [英] Unable to use input when multiprocessing in Python

查看:89
本文介绍了在 Python 中进行多处理时无法使用输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时运行 2 个进程.1 将继续每秒打印 'a',另一个将要求输入,当输入为 'Y' 时,第一个进程将停止打印 'a'.我对 Python 相当陌生,我无法弄清楚......

I want to run 2 processes at the same time. 1 will keep printing 'a' every second and the other will ask for an input and when the input is 'Y', the first process will stop printing 'a'. I am fairly new to Python and I can't figure it out...

这是我目前想到的:

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

这是我收到的错误消息:

This is the error message I'm getting:

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 的评论,因为他是正确的.

Expanding upon jasonharper's comment as he is correct.

有几个问题

  1. go 变量在进程之间共享.正如 Jason 建议您可以在 multiprocessing 中使用类似 Manager 的东西,以便在多个进程之间共享一个值.从技术上讲,该 go 变量将被复制到每个进程中,但不会在它们之间共享,因此一个进程中的更改不会被另一个进程看到.
  2. 同样,正如他提到的,您需要将 input(..) 拉入程序的主线程.此外,如果您使用的是 2.7,则需要使用 raw_input(..).
  3. 此外,如果您只检查一次标志然后退出,那么您可能会遇到 BrokenPipeError.
  1. The go variable is not shared among the processes. As Jason suggested you can use something like Manager in multiprocessing in order to share a value among multiple processes. Technically, that go variable will be copied over into each process but it won't be shared between them so a change in one process won't be seen by the other.
  2. Again, as he mentioned you need to pull the input(..) into the main thread of the program. Also, if you are on 2.7 you will need to use raw_input(..).
  3. Also, if you are only checking the flag once and then exiting then you'll likely hit a BrokenPipeError.

考虑到这一点,你可以尝试这样的事情:

Taking that in, you can try something like this:

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

这篇关于在 Python 中进行多处理时无法使用输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆