2

PHP continuously appends text to a file foo.log with mode "a". I want to read the contents of foo.log in python and truncate the lines that have been read, while avoiding concurrent access issues between python and PHP.

This answer states that os.rename is atomic on most platforms. Thus, would it be safe to accomplish my goal by simplty renaming foo.log to bar.log and then read bar.log into python? Or do I need to implement a more complex solution, such as a shared lockfile?

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
Agargara
  • 902
  • 11
  • 24
  • You should probably use a database or a message broker. – Loïc Nov 27 '17 at 00:52
  • @SynchroDynamic What do you mean by 'strategy object'? Also, renaming wouldn't create a duplicate file, it would simply erase the original. How would that lead to data corruption if the operation is atomic? – Agargara Nov 27 '17 at 01:15
  • @Loïc I'm avoiding using a database as the data is ephemeral and would just lead to an unnecessarily high amount of INSERT and DELETE operations. A message broker sounds useful for a lot of continuous messages between php and python, but overly complex for this particular problem. – Agargara Nov 27 '17 at 01:25

1 Answers1

1

Probably not if you fopen. What I use for logs is

file_put_contents( $filename, $data, FILE_APPEND|LOCK_EX);

That way you don't have an open resource handle, and it will create a file if no exists when it goes to write.

I tried it once with fwrite with about 15 different processes all logging stuff, wound up with half a file of NUL crap in it. So then I started using this.

http://php.net/manual/en/function.file-put-contents.php

  • FILE_USE_INCLUDE_PATH Search for filename in the include directory. See include_path for more information.

  • FILE_APPEND If file filename already exists, append the data to the file instead of overwriting it.

  • LOCK_EX Acquire an exclusive lock on the file while proceeding to the writing. In other words, a flock() call happens between the fopen() call and the fwrite() call. This is not identical to an fopen() call with mode "x".

Community
  • 1
  • 1
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38