0

I'm writing (or more precisely, appending) a real-time data stream to an instance of python's gzip module. If the program that's writing the stream crashes, and relaunches, I would like the stream to automatically be appended to the original file. Unfortunately this fails in practice, as I get an "unexpected end of file" error that corresponds to the exact point at which the original program crashed.

What's the underlying approach for handing this situation, as I can't imagine this should be a hard problem. My approach is outlined below:

f = gzip.GzipFile( 'filename_json.txt.gz' ), mode='at', compresslevel=9) while(something_is_true): f.write(stream['message'] + '\n') f.close() 

This runs continuously, but if the program crashes (or gets killed), the end-of-file operator never gets appended, and the gzip file becomes corrupt. In which case, any data appended after that point becomes unreadable.

Thanks!

1
  • You could use a try: finally: clause to ensure the file is always closed. Commented May 4, 2014 at 0:04

1 Answer 1

1
with gzip.open('filename_json.txt.gz', mode='at', compresslevel=9) as f: while something_is_true: f.write(stream['message'] + '\n') 

(This works for me on python 2.7.6)

But if that for some reason isn't working, you can do it the old fashioned way:

try: f = gzip.open('filename_json.txt.gz', mode='at', compresslevel=9) while something_is_true: f.write(stream['message'] + '\n') finally: f.close() 

Note that the error will still propagate with this code unless you catch the error. But the file will be closed.

Sign up to request clarification or add additional context in comments.

7 Comments

Does this function if the process is killed?
gzip.open and gzip.GzipFile work with with for me on python 2.7.6.
@AsksAnyway, no, this only "cures" the case where a Python exception occurs, not the case where the program is killed e.g., by a terminating signal or power loss.
How should we handle the case where the process is killed?
If you're killing it with e.g. ctrl-c that will send a sigint and raise a python exception. If you're killing it -9 or cutting power then of course it's not going to work.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.