0

It seems that starting a multiprocessing.Process() does not make my code work the way it should. See code below:

from multiprocessing import Process
from sys import exit



class Game():
    def __init__(self):
        self.cmd = ''
        self.balance = 1000

    def load_cmd(self):
        if self.cmd == 'quit':
            # quit game
            exit()

        if self.cmd == 'balance':
            print('Total balance:', self.balance)

    def start(self):
        while True:
            try:
                self.cmd = input('?> ')
                self.load_cmd()
                print('\n')
            
            except EOFError:
                self.cmd = 'quit'



if __name__ == '__main__':
    # load game
    game = Game()
    Process(target = game.start).start()

Instead of waiting for an input (while showing the ?> prompt), the script just keeps printing ?> without skipping lines. It is impossible to type anything. I tried running the function directly, without starting a process, and everything worked perfectly. Why is this happening?

martineau
  • 119,623
  • 25
  • 170
  • 301
montw
  • 85
  • 10
  • 2
    in short, each process has it's own stdin buffer, and won't read from the terminal stdin unless it is linked to it manually. – Ahmed AEK Jun 24 '22 at 17:43
  • You're constantly getting `EOFErrors` — for the reasons already stated — and simply setting `self.cmd = 'quit'` does not exit the infinite loop in `start()`. – martineau Jun 24 '22 at 17:51

0 Answers0