Reputation: 110163
I am delivering a video item to iTunes. When I run the command I get an output similar to:
$ command
[2013-01-09 11:02:14 PST] <main> DBG-X: 1%
[2013-01-09 11:02:15 PST] <main> DBG-X: 2%
[2013-01-09 11:02:16 PST] <main> DBG-X: 3%
...etc...
How would I go about updating a database field in my application to show the progress of this delivery? I imagine I would re-direct to a log file using something like stdout=subprocess.PIPE, stderr=subprocess.PIPE
.
But how would I get this in real time to update the db?
Upvotes: 1
Views: 93
Reputation: 113955
I would do this (there are probably other, better ways):
n
seconds and remember the last line in that file.Note that this is only almost real-time, as you only check the file every n
seconds
Code:
progress = 0
lastLine = ''
n = 5 # seconds
while progress < 100:
with open('path/to/logfile') as logfile:
for newLine in logfile:
pass
if newLine.strip() != lastLine:
lastLine = newLine.strip()
writeToDatabase(lastLine)
time.sleep(n)
Hope this helps
Upvotes: 2