如何在Python中一次跟踪多个文件?

如何在Python中一次跟踪多个文件?,python,Python,我有多个日志文件,我想让python持续监视特定事件。我能够对单个日志文件执行类似于tail-f的操作。这是代码: import time def follow(thefile): thefile.seek(0,2) while True: line = thefile.readline() if not line: time.sleep(0.1)

我有多个日志文件,我想让python持续监视特定事件。我能够对单个日志文件执行类似于tail-f的操作。这是代码:

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

但正如您在代码中看到的,我有多个日志文件,如何一次跟踪多个日志文件。如何做到这一点?

创建一个跟踪单个文件的线程。 然后实例化多个线程实例。

查看该模块。