in reply to Writing to the same file from different instances
Another way to handle a problem like this is to create a simple server that reads from (say...) a socket, and writes what it reads into the file. That way, only one process is actually writing to the file, and it’s doing it asynchronously. If you have a bunch of processes who basically need “to log something and move on,” but who do not need to subsequently and immediately read what is to be written, then this is a useful technique. (Operating systems usually delegate a “logger” system process for this very purpose and reason.) You cannot predict the exact order in which the writes will occur, as before, but the entire messy issue of “locking” is completely avoided.