tail -F 的源码是采用 inotify + select 方式来实现
python 的 pyinotify 库对 inotify 进行了封装,但貌似性能不是很好。
实现 tail-F: https://github.com/masterzh01/tail-F
1
necomancer 2016-09-03 02:24:50 +08:00
from sys import argv
import collections o = open(argv[1], 'r') print(''.join(collections.deque(o, 5)).strip('\n')) # last 5 lines o.seek(0,2) # jump to last line while 1: ....line = o.readline() ....if line: ........print(line.strip('\n')) 就可以了吧…… |
2
masterzh01 OP @necomancer 不行, cpu , rotate.....( tail -F )
|
3
hasdream 2016-09-03 10:09:33 +08:00
```
import time import time def follow(thefile): thefile.seek(0,2) # Go to the end of the file while True: line = thefile.readline() if not line: time.sleep(0.1) # Sleep briefly continue yield line # Example use if __name__ == '__main__': logfile = open("access-log") for line in follow(logfile): print line, ``` |
4
necomancer 2016-09-03 13:20:33 +08:00
@masterzh01 CPU usage 问题加上 sleep 就行:
from sys import argv import collections import time o = open(argv[1], 'r') print(''.join(collections.deque(o, 5)).strip('\n')) # last 5 lines o.seek(0,2) # jump to last line while 1: ....line = o.readline() ....if not line: ........time.sleep(0.1) ........continue ....print(line.strip('\n')) |
5
masterzh01 OP @hasdream @necomancer 事件驱动方式会更好些?
|
6
hasdream 2016-09-03 23:55:27 +08:00 via Android
@masterzh01 如果用事件来做基本没延迟 不过要复杂点还要对事件处理
|