Tom Freezers
Tom Freezers

Reputation: 227

How can you tail -f multiple files at once in Python?

I have multiple log files I would like python to watch continuously for specific events. I was able to do something similar to a "tail -f" to a single log file. Here is that code:

import time

def follow(thefile):
        thefile.seek(0,2)
        while True:
                line = thefile.readline()
                if not line:
                        time.sleep(0.1)
                        continue
                yield line

if __name__ == '__main__':
        logfile1 = open("/connector1/logs/agent.out.wrapper.log","r")
        logfile2 = open("/connector2/logs/agent.out.wrapper.log","r")
        logfile3 = open("/connector3/logs/agent.out.wrapper.log","r")
        loglines = follow(logfile1)
        for line in loglines:
            print line

But as you can see in the code I have multiple log files how to tail more than one at a time. How can this be done?

Upvotes: 0

Views: 574

Answers (1)

Gambit Support
Gambit Support

Reputation: 1473

Create a Thread that tails a single file. Then instantiate multiple Thread instances.

Upvotes: 1

Related Questions