Great God Guide works:
#!/usr/bin/python
#coding: UTF8
From Functools Import Wraps
From time import sleep
Import OS
Restart= ' pm2 RESTART ios-push '
# Coroutine First call with Next statement
def coroutine (FN):
@wraps (FN)
def wrapper (*args,**kwargs):
RET=FN (*args,**kwargs)
Next (ret)
return ret
Return wrapper
def follow (File,target):
‘‘‘
Unix-like Tail-f, but accepts a target, has new data, and gives it the target handle.
‘‘‘
File.seek (0,2) # directly to the last line of the file, similar to Tail-f-N 0
While True:
Line=file.readline () #读取行
If not line:
Sleep (0.5) # If the line is empty, go directly to sleep 0.5s, then continue
Continue
Target.send (line) # If you have new data, give it to target processing
def action ():
If Os.system (RESTART) = = 0:
Print (' Ios-push restart success! ')
#print (' restart ')
@coroutine
def grep (pattern):
‘‘‘
grep is a coroutine that takes a pattern parameter and needs to send data from the outside, and if the accepted data matches the pattern passed, then the action function is called
‘‘‘
While True:
Line = yield
If pattern in line:
Action ()
if __name__ = = ' __main__ ':
Follow (open (' Error.log ', ' R '), grep (' Maxlistenersexceededwarn '))
Python Realtime filter Log script