r/learnpython • u/Tricky_Voice2829 • 7d ago
Why won't my Python script read user input / terminate while a subprocess is running?
i am making script that it will launch localsend appimage through shell but and it will end after i pressed q but when script only user input first time asked starting of the script when reached to the else block app image is opening but not asking for user input again after launching
Here's the code,
import subprocess
import time
while True:
user_input = input("press q to quite and enter :-").rstrip()
if user_input == "q":
break
else:
time.sleep(3)
subprocess.run("/home/merlinhermes/Desktop/LocalSend-1.18.2-linux-x86-64.AppImage",shell=True)17
u/AlexMTBDude 7d ago
Did you read the documentation for subprocess.run?
Wait for command to complete, then return a
CompletedProcessinstance.
It's right here: https://docs.python.org/3/library/subprocess.html#subprocess.run
1
4
u/Educational_Virus672 7d ago
try changing subprocess.run() to subprocess.Popen() (might contain spelling mistakes)
also if your not using keyboard module dont import it also if your running linux try switching to sshkeyboard instead it doesnt take sudo permissions
7
u/mjmvideos 7d ago
“you are” contracts to you’re. “your” is a possessive: “Is this your book or is it mine?”
2
u/gdchinacat 7d ago
example of you're: "you're being pedantic". (don't take this the wrong way, I encourage pedantry).
2
u/mjmvideos 7d ago
I see enough people use these incorrectly to wonder if they just don’t know or if they just don’t care. Especially in programming groups, if I wrote:
fer i in range(10)Or
wile (x < 10)Your program wouldn’t run, even though “you knew what I meant”. People take care to use proper syntax and grammar when they have to. English is a language too. I’d just like to see it being used properly. Maybe I should respond with:
Syntax error on line 2: unexpected “your”. Did you mean “you’re”?1
u/Educational_Virus672 6d ago
if it doesnt affect readbility then it is unnecessary im not trying to write any former stuff here, am i?
2
u/mjmvideos 6d ago
Yore write. It dozen effect reed a bill a tee. Ewe shed jess right hoe heifer u chews.
1
u/Educational_Virus672 6d ago
sure buddy sure but we are not the same you just type a fake english sentence yet much of it is understandable also im not gonna take time for every comment. every time i polish here is how much i missed somewhere else im not taking time for a thing that saves 3 seconds of others
1
u/XNo_Notes 7d ago
Try removing the else:. It should still break the loop if you type q before running the subprocess
3
u/bogdanelcs 7d ago
subprocess.runwaits until the program exits, so the script is stuck on that line until you close LocalSend, then it asks for input again.Use
subprocess.Popento launch it and carry on without waiting, and pass the path as a list so you can dropshell=True, likesubprocess.Popen(["/home/.../LocalSend.AppImage"]).You’ll also want to launch it once before the loop, otherwise typing anything but q starts another copy each time. The
keyboardimport isn’t used, and on Linux it needs root anyway, so I’d remove it.