0

I am trying to track a user's decision before a certain time - and to do this, I am asking for input, while setting a timer. I am trying to use these two concurrently, so that the timer will go while there is a query for input. My solution at the moment is not working - could someone please tell me what I need to do to succeed here?

# pft4.py

from multiprocessing import Process
import time

def attack_request():
    attack_query = input("Do you want to attack? ")
    print(attack_query)

def attack_timer():
    timer = 0
    for i in range(3): 
        timer += 1
        time.sleep(1)
        print("Timer updated: " + str(timer))
    return timer

def main():
    process_1 = Process(target = attack_request)
    process_2 = Process(target = attack_timer)
    process_1.run(); process_2.run()
    process_1.join(); process_2.join()
    # attack_request()
    # attack_timer()

if __name__ == "__main__":
    main()
Caspian Ahlberg
  • 976
  • 4
  • 12

1 Answers1

0

You have to call Process.start(), not Process.run(). run() just executes the process's main function. It does not arrange for this function to be run in a different process. start() does exactly that. See the reference documentation of the Process class.

In your code, process_1.run() will execute attack_request() and block until that returns. Then it does the same with attack_timer(). If you use start() instead then both will be executed simultaneously.

See also this related post: Python multiprocessing stdin input Getting input from stdin may not be simple. Probably better to use threading, like others suggested.

Daniel Junglas
  • 5,265
  • 1
  • 3
  • 18