Python Daemon Thread Exits But Process Still Run In The Background
Solution 1:
It is likely because you are using os.system
in your thread. The spawned process from os.system
will stay alive even after the thread is killed. Actually, it will stay alive forever unless you explicitly terminate it in your code or by hand (which it sounds like you are doing ultimately) or the spawned process exits on its own. You can do this instead:
import atexit
import subprocess
deviceIp = '100.21.143.168'
fileTag = 'somefile'# this is spawned in the background, so no threading code is needed
cmdTorun = "adb logcat > " + deviceIp +'_'+fileTag+'.log'
proc = subprocess.Popen(cmdTorun, shell=True)
# or register proc.kill if you feel like living on the edge
atexit.register(proc.terminate)
# Here is where all the other awesome code goes
Since all you are doing is spawning a process, creating a thread to do it is overkill and only complicates your program logic. Just spawn the process in the background as shown above and then let atexit
terminate it when your program exits. And/or call proc.terminate
explicitly; it should be fine to call repeatedly (much like close
on a file object) so having atexit
call it again later shouldn't hurt anything.
Post a Comment for "Python Daemon Thread Exits But Process Still Run In The Background"