3

私は(主に)次のコードを持っています:

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

2番目のraw_inputは、EOF例外をスローします。

     ret = original_raw_input(prompt)
     EOFError: EOF when reading a line

これを修正する方法はありますか?複数のプロンプトを使用できませんか?

4

1 に答える 1

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

ただし、JFセバスティアンのソリューションを使用して、それを複製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()
于 2012-12-09T13:05:56.920 に答える