Python decorator implements monitoring of exception code occurrences

Source: Internet
Author: User
Tags python decorator

Abnormal, should not exist, but we sometimes encounter such a situation, such as when we monitor the server, every second to collect information, then one second did not collect the information we want, but the next second acquisition, and then every time the acquisition can be collected, so that a collection is not, We should not for this time the acquisition of the analysis, the situation can be said that cannot be repeated, we cannot avoid, because the external factors too much, we can not control these outside factors, so we will have this demand, a period of time the frequency of how many times, we can show an alarm, or say, The frequency of occurrence within a period of time reaches our abnormal license range we think this is an anomaly and we can send an alarm.

So how do we come to realize that, I think of the adorner, when the program executes to an exception, I record time, write to the file, and then read the last fifth time, the difference between the two timestamps is less than 60s, I think such can send an alert, if greater than 60s, it is not enough to issue our warning,

So let's take a look at how our code should be written.

ImportDatetime,time,randomdefMake (func):defMak (*args,**Kwargs):Try: Func (*args,**Kwargs)except: With open ('Except.txt','A +') as F:except_time=Datetime.datetime.now () f.writelines (Except_time.strftime ('%y-%m-%d%h:%m:%s')+'\ n') F.close () with open ('Except.txt','RB') as M:Try: Date=m.readlines () [ -5].decode ('Utf-8') NE= (Date.split ('\ r \ n') [0]) F1=datetime.datetime.strptime (NE,'%y-%m-%d%h:%m:%s')                    if(EXCEPT_TIME-F1). seconds<6:                        Print('Unusual!!! Fail')                    Else:                        Print('Normal! ') M.close ()except:                    Print('crossing the border means our experiment was successful.')    returnMak@makedefBeijing (i,m):Print(i/m)if __name__=="__main__":     whiletrue:f=random.choice ([0,1,2,3]) n=random.choice ([0,1,2,3]) Beijing (f,n) time.sleep (0.3)

This allows us to implement exception monitoring for a program, and we run the code for this exception monitoring. ,

We can see that our code is working, so let's try it, we monitor the code of multiple programs, can our script be implemented?

@make def Shanghai (i,m):     Print (i/m) @make def Rizhao (i,m):     Print (i/m) @make def Zhengzhou (i,m):     Print (i/m)

We add so few methods, and we go to run them,

We can see that as long as there is an exception, our program will be recorded, of course, this is not normal use of our work,

Later, you can put the Exception Monitoring section here in practice, and I want to combine the exception monitoring scripts that I wrote earlier.

Python decorator implements monitoring of exception code occurrences

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.