首页 > 解决方案 > 使用多处理时,输入函数在 python 中过早结束

问题描述

我有一些这样的代码:

from multiprocessing import Process

def foo():
    while True:
        f=input('Input: ')
        print('Why doesn't this print?')

if __name__=='__main__':    
    p1 = Process(target=bruh)
    print('this prints')
    p1.start()
    print('and so does this')

但是运行时,我收到此错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()

  File "C:\Program Files (x86)\Python38-32\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)

  File "C:\Users\(myUsername)\Desktop\program.py", line 739, in foo
    f=input('still running')

EOFError: EOF when reading a line

我相信这是因为进程在到达输入函数时结束,导致它给出字段结束错误,但我不知道。请帮忙!

(使用python 3.8.3)

标签: pythonpython-3.xmultithreadingmultiprocessing

解决方案


EOF 错误通常是由行尾的错误引起的,例如缺少括号或类似的东西。尝试将 if 缩进到函数内部的循环中。

如果这不起作用,也许你写print('Why doesn't this print?')而不是print("Why doesn't this print?")


推荐阅读