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?

user1862770
  • 307
  • 1
  • 4
  • 13
  • That is, I am not even asked the raw-input, the line is not even printed. – user1862770 Dec 09 '12 at 11:47
  • I fixed that by putting the raw_input in the main, too. – user1862770 Dec 09 '12 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() – user1862770 Dec 09 '12 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? – user1862770 Dec 09 '12 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`. – unutbu Dec 09 '12 at 12:48
  • By the way, this may be helpful for your main problem: http://stackoverflow.com/a/8981813/190597 – unutbu Dec 09 '12 at 12:50
  • If you found the solution to your problem, please formulate it as an answer and accept it — or delete the question, if you feel that it's no longer valid. – user4815162342 Dec 09 '12 at 12:51
  • possible duplicate of [Python command line input in a process](http://stackoverflow.com/questions/5697305/python-command-line-input-in-a-process) – n611x007 Mar 13 '15 at 13:26

1 Answers1

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()
Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677