David542
David542

Reputation: 110163

Real-time progress from log file

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

Answers (1)

inspectorG4dget
inspectorG4dget

Reputation: 113955

I would do this (there are probably other, better ways):

  1. Check the file every n seconds and remember the last line in that file.
  2. If the last line has changed, it means that some progress has been made since the last time you checked it.
  3. Once you've detected such progress, you should be able to trigger the function that writes to your database.

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

Related Questions