'python subprocess not exiting properly
I am trying to;
- Call a bash script within the python script to listen to keyboard inputs
- When a specific key is pressed, return to the main program (python) and #do something
Here is my main python program;
import subprocess
a = subprocess.run("./s.sh")
print(a.returncode)
Here is s.sh
#!/bin/bash
xinput test-xi2 --root 3 | grep -A2 --line-buffered RawKeyRelease | while read -r line;
do
if [ "$line" == "detail: 38" ]; then
echo "hello world"
exit 38
fi
done
I got this from https://stackoverflow.com/a/56605941/12616968. I need to listen to the keyboard press in the background.
Here when key 'a' is pressed first "hello world" then its number (that is 38) should be printed on the shell. Here is the output
➜ $python3 test.py
ahello world
aa38
"hello world" is printed as expected. But for the exit code (38) I need to press the keys (this time anything, it need not be 'a') two more times. How can I avoid doing that?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
