Python implements the method for monitoring program execution time and writing it into logs, and the python Monitoring Program
This example describes how to implement the execution time of a monitoring program in Python and write it into logs. Share it with you for your reference. The specific implementation method is as follows:
#/Usr/bin/python #-*-coding: UTF-8-*-from time import timedef logged (when): def log (f, * args, ** kargs ): print '''called: functions: % s args: % r kargs: % r''' % (f, args, kargs) def pre_logged (f ): def wrapper (* args, ** kargs): log (f, * args, ** kargs) return f (* args, ** kargs) return wrapper def post_logged (f ): def wrapper (* args, ** kargs): now = time () try: return f (* args, ** kargs) finally: log (f, * args, ** kargs) Print "time delta: % s" % (time ()-now) return wrapper try: return {"pre": pre_logged, "post": post_logged} [when] handle T KeyError, e: raise ValueError (e), 'must be "pre" or "post" '@ logged ("post") def hello (name): print "hello ,", namehello ("world! ") ''' Is equivalent to: hello = logged (" post ") (hello (" world! "))'''
I hope this article will help you with Python programming.