Reputation: 127
I was trying to use subprocess to extract lines from log file. The intention here is to extract the logs while some program is getting executed and wait for some time copy all the logs to another file.
#!/bin/python
import threading
import time, subprocess
class GetLogs(threading.Thread):
'''
Get the developer logs
'''
def __init__(self):
'''
init
'''
self.stop = False
threading.Thread.__init__(self)
print "Initialised thread"
def run(self):
'''
Collect the logs from devlog
'''
command = "tail -f /var/log/developer_log | tee /tmp/log_collected"
response = subprocess.check_output(command.split(), shell=True)
print "Subprocess called"+str(response)
while ( self.stop is False ):
time.sleep(1)
print "Continuing"
continue
print "Finished the log file"
gl = GetLogs()
gl.start()
##Do some activity here
print "Sleeping for 10 sec"
time.sleep(10)
gl.strop = True
print "Done"
This doesn't work - program gets stuck.
Upvotes: 0
Views: 218
Reputation: 3443
subprocess.check_output() waits for all the output. It waits for the child process to exit or close its STDOUT stream.
tail -f
never exits and never closes its STDOUT stream. Therefore none of the lines of code below the call to check_output() will execute.
As the warning about deadlocks in https://docs.python.org/2/library/subprocess.html suggests, look at using Popen() and communicate() instead.
Upvotes: 1