3

I have (in the main) the following code:

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

The methods called above basically do as follows:

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

It works fine, the server spawns and so does the client.

Only yesterday I added in client.Client() the following line:

self.ip = raw_input("IP-Adress: ") 

The second raw_input throws an EOF -exception:

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

Is there a way to fix this? Can I not use more than one prompt?

8
  • That is, I am not even asked the raw-input, the line is not even printed. Commented Dec 9, 2012 at 11:47
  • I fixed that by putting the raw_input in the main, too. Commented Dec 9, 2012 at 12:16
  • However there is now sth else puzzling me: ip = raw_input("IP-Adresse: ") print (ip) clientprozess = Process (target = spawn_client, args = ip) clientprozess.start() Commented Dec 9, 2012 at 12:17
  • I now get the mistake that spawn_client takes one Argument (one only) but I had given it 9. How can that be? ip should be ONE string, should not it? Commented Dec 9, 2012 at 12:17
  • Use args = (ip, ). args is later used like this: func(*args), so whatever you assign to args gets unpacked as individual arguments passed to func. Commented Dec 9, 2012 at 12:48

1 Answer 1

4

As you've already determined, it is easiest to call raw_input from the main process only:

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

However, using J.F. Sebastian's solution it is also possible to duplicate sys.stdin and pass it as an argument to the subprocess:

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.