If avoiding data corruptions is that important why not use a transaction based stuff? i.e. Write different files for the data and after you have received the finished data try to commit them to the main-file with its own script.
But it is just an idea!
JanneVee