5

For instance:

def read_file(f):
    with open(f, 'r') as file_to_read:
        while True:
            line = file_to_read.readline()
            if line:
                yield line
            else:
                time.sleep(0.1)

The generator is consumed by another function:

def fun_function(f):
    l = read_file(f)
    for line in l:
        do_fun_stuff()

A use case would be reading an infinitely updating text file like a log where new lines are added every second or so.

As far as I understand the read_file() function is blocking others as long as something is yielded. But since nothing should be done unless a new line is present in the file, this seems to be okay in this case. My question would be if there are other reasons not to prefer this blocking pattern (like performance)?

  • 2
    I don't understand the question. Too convoluted. Or opinion based: ("My question would be if there are other reasons not to prefer this blocking pattern (like performance)"). Why making it so complex? why do you need that? – Jean-François Fabre Nov 12 '19 at 20:17
  • 1
    main problem I'm seeing: at the end of the file `read_file` blocks forever in a non-busy loop without any possibility to exit. – Jean-François Fabre Nov 12 '19 at 20:19
  • 2
    @sjne I suggest you add that in your question. Also updating a file while you're reading from it doesn't work on all systems (ex: windows) – Jean-François Fabre Nov 12 '19 at 20:23
  • When do you want your program to stop reading? Even if it's a long-running daemon process, you should have some way of signalling for it to shut down safely. – kaya3 Nov 12 '19 at 20:25
  • @kaya3 Well at the moment just killing the process from the command line. I really haven't thought about it. –  Nov 12 '19 at 20:29
  • [Examples of reading from a log file](https://stackoverflow.com/questions/3290292/read-from-a-log-file-as-its-being-written-using-python)--somewhat similar to what you're doing. [Similar to Unix tail](http://code.activestate.com/recipes/157035-tail-f-in-python/) in Python. – DarrylG Nov 12 '19 at 20:31

0 Answers0