Skip to content

Commit 2d22adc

Browse files
author
guiyin.xiong
committed
日志分析,没怎么看懂。
1 parent 4794b52 commit 2d22adc

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

comyn_lesson/日志分析.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,99 @@ In [15]: now + datetime.timedelta(seconds=5)
892892
Out[15]: datetime.datetime(2017, 2, 26, 20, 38, 14, 43360)
893893
```
894894

895+
---
896+
---
897+
898+
# 同时处理多个文件、多进程
899+
900+
需要流式的读取文件:
901+
902+
$ tail -f ==> long polling机制(定时地去检查并读取文件到末尾)
903+
904+
```python
905+
import time
906+
def load(path):
907+
with open(path) as f:
908+
while True:
909+
for line in f:
910+
try:
911+
yield extract(line)
912+
except:
913+
pass
914+
915+
time.sleep(0.1)
916+
```
917+
918+
919+
```python
920+
# 从文件1中逐行读取,并写入到文件2中;
921+
import sys
922+
import time
923+
924+
with open(sys.argv[1]) as r:
925+
with open(sys.argv[2],'w') as w:
926+
for line in r:
927+
w.write(line)
928+
w.flush()
929+
time.sleep(1)
930+
```
931+
932+
933+
## inotify --> inode notify
934+
935+
监控文件系统的变化:
936+
937+
利用inotify实现tail -f的功能;
938+
939+
文件系统里:
940+
941+
文件的创建、删除、移动、修改,都会发出inotify event。
942+
943+
* watchdog(linux) 是inotify中实现得比较好的一种;可用于做文件同步、备份;
944+
* fsevent(macos)
945+
946+
947+
```python
948+
# 不用再定时去查看文件,利用文件修改的event
949+
import sys
950+
import time
951+
import logging
952+
from watchdog.observers import Observer
953+
from watchdog.events import LoggingEventHandler
954+
955+
if __name__ == "__main__":
956+
logging.basicConfig(level=logging.INFO,format='%(asctime)s %(message)s',datefmt='%Y-%m-%d %H:%M:%S')
957+
path = sys.argv[1] if len(sys.argv) > 0 else '.'
958+
959+
handler = LoggingEventHandler()
960+
observer = Observer()
961+
962+
observer.schedule(handler,path,recursive=True)
963+
observer.start()
964+
try:
965+
while True:
966+
time.sleep(1)
967+
except KeyboardInterrupt:
968+
observer.stop()
969+
observer.join()
970+
```
971+
972+
比较经典的12种设计模式,不要为了设计模式而使用设计模式;
973+
974+
标准库要看一遍;
975+
976+
977+
* datetime
978+
* re
979+
* argparse
980+
* logging
981+
* pathlib
982+
* psutil
983+
* watchdog
984+
* difflib
985+
986+
987+
988+
文档和注释
989+
建议多谢文档,少写注释;
990+

0 commit comments

Comments
 (0)