Python 如何将多处理与input()函数一起使用并避免;EOFError:读取一行时的EOF;?
我目前正在制作一个Python程序,该程序应该使用多处理,其中一个函数处理UI,另一个函数处理更新数据文件。当我的UI函数调用函数Python 如何将多处理与input()函数一起使用并避免;EOFError:读取一行时的EOF;?,python,multiprocessing,user-input,eoferror,Python,Multiprocessing,User Input,Eoferror,我目前正在制作一个Python程序,该程序应该使用多处理,其中一个函数处理UI,另一个函数处理更新数据文件。当我的UI函数调用函数input()进行用户输入时,显示了读取一行时出错的原因EOF或:EOF 下面是简化的代码,可以作为示例,在我的实际程序中创建完全相同的错误: import multiprocessing import time # Class with methods used to print strings. class Printing: # Setting of
input()
进行用户输入时,显示了读取一行时出错的原因EOF或:EOF
下面是简化的代码,可以作为示例,在我的实际程序中创建完全相同的错误:
import multiprocessing
import time
# Class with methods used to print strings.
class Printing:
# Setting of initial values is ignored.
def __init__(self):
pass
# Continuously calls for user input that is then printed.
def printInput(self):
while True:
string = input("Enter a string: ")
print(string)
# Continuously prints the character "X"
def printXs():
while True:
time.sleep(1) # Just used to slows the printing output.
print("X")
# Execution of script when told to run.
if __name__=='__main__':
mp1 = multiprocessing.Process(target=printXs)
mp2 = multiprocessing.Process(target=Printing().printInput)
mp1.start()
mp2.start()
当读取第14行的一行
时,或者换句话说,输入的一段代码(“输入字符串”)
标准输入流(stdin)在创建时未附加到流程中,由此产生的错误是EOF:EOF。由于由多个进程从同一个流中读取输入是没有意义的,因此默认情况下假定stdin将由主进程以独占方式读取
在您的情况下,您只需要从一个流程中获取输入,但您希望它是您生成的流程之一。在这种情况下,您可以按如下方式连接stdin:
def printInput(self):
sys.stdin = open(0) # <--- Here's the magic line...you'll need to "import sys" above too
while True:
string = input("Enter a string: ")
print(string)
def打印输入(自身):
sys.stdin=open(0)#很高兴我能帮忙。我很想得到一个绿色对勾的点头没问题,伙计忘了。