我(主要)有以下代码:
status = raw_input("Host? (Y/N) ")
if status=="Y":
print("host")
serverprozess = Process(target= spawn_server)
serverprozess.start()
clientprozess = Process (target = spawn_client)
clientprozess.start()
上面提到的方法基本上如下:
def spawn_server():
mserver = server.Gameserver()
#a process for the host. spawned if and only if the player acts as host
def spawn_client():
myClient = client.Client()
#and a process for the client. this is spawned regardless of the player's status
它工作正常,服务器产生,客户端也是如此。
仅在昨天我在client.Client()中添加了以下行:
self.ip = raw_input("IP-Adress: ")
第二个raw_input抛出一个EOF -exception:
ret = original_raw_input(prompt)
EOFError: EOF when reading a line
有没有办法解决这个问题?我可以不使用多个提示吗?
答案 0 :(得分:4)
正如您已经确定的那样,最简单的方法是从主流程调用raw_input
:
status = raw_input("Host? (Y/N) ")
if status=="Y":
print("host")
serverprozess = Process(target= spawn_server)
serverprozess.start()
ip = raw_input("IP-Address: ")
clientprozess = Process (target = spawn_client, args = (ip, ))
clientprozess.start()
但是,使用J.F. Sebastian's solution也可以复制sys.stdin
并将其作为参数传递给子流程:
import os
import multiprocessing as mp
import sys
def foo(stdin):
print 'Foo: ',
status = stdin.readline()
# You could use raw_input instead of stdin.readline, but
# using raw_input will cause an error if you call it in more
# than one subprocess, while `stdin.readline` does not
# cause an error.
print('Received: {}'.format(status))
status = raw_input('Host? (Y/N) ')
print(status)
newstdin = os.fdopen(os.dup(sys.stdin.fileno()))
try:
proc1 = mp.Process(target = foo, args = (newstdin, ))
proc1.start()
proc1.join()
finally:
newstdin.close()