3

I want to run 2 processes at the same time. 1 will keep printing 'a' every second and the other will ask for an input and when the input is 'Y', the first process will stop printing 'a'. I am fairly new to Python and I can't figure it out...

This is what I came up with so far:

from multiprocessing import Process
import time

go = True

def loop_a():
    global go

    while go == True:
        time.sleep(1)
        print("a")

def loop_b():
    global go
    text = input('Y/N?')

    if text == 'Y':
        go = False

if __name__ == '__main__':
    Process(target=loop_a).start()
    Process(target=loop_b).start()

This is the error message I'm getting:

Process Process-2:
Traceback (most recent call last):
  File "C:\Users\Tip\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 249, in _bootstrap
    self.run()
  File "C:\Users\Tip\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "F:\ProgrammingTK\PROGproject\test.py", line 15, in loop_b
    text = input('Y/N?')
EOFError: EOF when reading a line
2
  • The multiprocessing module intentionally closes the standard input of all processes it creates: it would otherwise be indeterminate as to which one actually received any input. You could fix this by doing the input loop in the main program, but your code still wouldn't work - your global go is NOT shared between processes. You would need to use multiprocessing.Value or any of various other mechanisms that are explicitly shared between processes. Commented Nov 6, 2017 at 19:22
  • here is another solution:stackoverflow.com/questions/17859140/… Commented Sep 10, 2018 at 16:04

1 Answer 1

2

Expanding upon jasonharper's comment as he is correct.

There are a couple issues

  1. The go variable is not shared among the processes. As Jason suggested you can use something like Manager in multiprocessing in order to share a value among multiple processes. Technically, that go variable will be copied over into each process but it won't be shared between them so a change in one process won't be seen by the other.
  2. Again, as he mentioned you need to pull the input(..) into the main thread of the program. Also, if you are on 2.7 you will need to use raw_input(..).
  3. Also, if you are only checking the flag once and then exiting then you'll likely hit a BrokenPipeError.

Taking that in, you can try something like this:

from multiprocessing import Process, Manager
import time


def loop_a(go):
    while True:
        # run forever and print out the msg if the flag is set
        time.sleep(1)
        if go.value:
            print("a")

if __name__ == '__main__':
    # shared value flag
    manager = Manager()
    go_flag = manager.Value('flag', True)

    # other process that is printing
    Process(target=loop_a, args=(go_flag,)).start()

    # normal main thread; toggle on and off the other process
    while True:
        text = input('Stop Y/N?')
        if text == 'Y':
            go_flag.value = False
            print("Changed the flag {}".format(go_flag.value))
        else:
            go_flag.value = True
            print("Changed the flag {}".format(go_flag.value))
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.