Skip to content
Advertisement

How do I pause other processes when user input / confirmation is required Subprocess Python

If I have a script (process run.py) that will run 3 subprocesses A.py, B.py, C.py (3 scripts called at same time through process run.py).

p= subprocess.Popen(['python3', '-u', 'A.py'])
p2 = subprocess.Popen(['python3', '-u', 'B.py'])
p3 = subprocess.Popen(['python3', '-u', 'C.py'])

The operation is fine. However say in b.py i have a line such as

reply = input( do you want to continue this action y/n :)
if reply.lower == 'yes':
take this action()
elif reply.lower == 'no' :
take other action()

(pref) How can i get subprocess of A.py and c.py to wait/pause for 5seconds, in order for user to enter y or no or wait in order for user to enter y or no

A basic confirm prompt in terminal. However the problem I face is A.py B.py running not allowing the confirmation which would start a different task/process/script.

Guidance here would be warmly welcomed.

Advertisement

Answer

You must use an external system to let the processes communicate between them.

The external system can be as simple as writing in a file some codes and having all the processes checking the state of the files.

B.py

with open("my.lock", "w") as f:
    f.write("a")
reply = input( do you want to continue this action y/n :)
if reply.lower == 'yes':
take this action()
elif reply.lower == 'no' :
take other action()
os.remove("my.lock")

A.py or C.py

import os
import time

while os.path.isfile("my.lock"):
   time.spleep(1)
# then your logic

A more sophisticated solution would be to have a message broker (RabbitMQ, Kafka…).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement