"Follow" a file being written to by another process

Is there a good way to have a Prolog thread reading from a file as it’s being written to by another process? I’m trying to open the file that the process is writing to, then using stream_to_lazy_list/2 & passing that to my DCG to read a line at a time as they come in, but the lazy list never seems to actually get anything – if I try to print the lazy list before reading, it’s just the attributed variable, with no elements on the list.

If I sleep for a second before opening then I can see the input that’s come in before that, but I don’t get any updates after that.

You can use open/4 with the option eof_action(reset). That returns end_of_file (or -1 for get_code/2 and friends) upon reaching end of file. A subsequent read call may yield more data. That won’t work with the lazy stream predicates though. An option that can work is to use pipe/2, turn the reading end into a lazy stream and create a thread that reads from the file and writes to the pipe and waits for a polling time each time it hits end-of-file.

If you do not really need the file, I’d consider pipes, unix domain socket or normal socket.

Thanks Jan, I’ll give that a try. A socket or something definitely would be easier, but unfortunately, I think it does need to be a file in this case.

If Linux is the target you can also use the inotify add-on to avoid polling.

Oh perfect! I didn’t realize there was a pack – I had already started writing a foreign predicate to wrap inotify, but that’s much easier :smiley: