Basic Primer _python-modules and packages. What are the best practices for watchdog event monitoring in operational development?

Source: Internet
Author: User

Brief introduction:

Description: This module is a cross-platform Py Library and Shell tool that can monitor file system events (Add/remove/Modify)


Quick installation:

PIP Install--upgrade watchdog

Log records:

Event_handler = Loggingeventhandler (), Event_handler

Description: Create a log processing handle, in fact, Loggingeventhandler is inherited from the FileSystemEventHandler class, just rewrite the change and delete the callback function, directly call logging module write to the corresponding logging configuration target

Description: This module for us to achieve a Watchdog.events.LoggingEventHandler class, can be directly with the logging module, you can simply record additions and deletions to change

#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman#  oschina: http://xmdevops.blog.51cto.com/# purpose:# "" "#  Description:  Import public module Import timeimport  loggingfrom watchdog.observers import Observerfrom watchdog.events import  loggingeventhandler#  Description:  Import other modules if __name__ ==  ' __main__ ':     Logging.basicconfig (level=logging. debug,                         format= '% (asctime) s - % (message) s ',                          datefmt= '%y-%m-%d %h:%m:%s ')     logging.info (' start watching. ')     event_handler = loggingeventhandler ()     watcheR = observer ()     watcher.schedule (event_handler=event_handler, path= '. '),  recursive=true)     watcher.start ()     try:         while True:             time.sleep (1)     except KeyboardInterrupt, e:         watcher.stop ()     watcher.join ()

Description: Loggingeventhandler directly call Logging.info write log, and logging is so powerful to support thread-safe log module, so can organically combine to achieve more powerful function, watchdog is very simple, first from WATCHDOG.OBSERVERS Import observer Imports Observer class, and then instantiates the call schedule only by passing three parameters, the first parameter is the instance processing handle, the second parameter is to monitor the address, the default does not recursively monitor, Only recursive detection when the recursive=true is specified, as to the start/stop/join of the thread object I will not say more ~ Yes, the above for loop is mainly to catch the CTRL + C exception, call Watch.stop () to let the thread gracefully exit ~


Callback Processing:

Event_handler = FileSystemEventHandler (), Event_handler

Note: Since FileSystemEventHandler is a base class, and there is no specific implementation of the On_any_event/on_created/on_deleted/on_modified/on_moved method, Therefore, it is not usually instantiated directly as an event handler, but rather a custom class that inherits it and then implements those callback functions.

#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman#  oschina: http://xmdevops.blog.51cto.com/# purpose:# "" "#  Description:  Import public module Import timefrom  watchdog.events import FileSystemEventHandler#  Instructions:  Import other modules def check_modification (func):     def wrapper (self, event):         print u ' event statics:         Event type: {}          whether directory: {}         file path:  {}         ". Format (Event.event_type, event.is_directory,  event.src_path)     return wrapperclass customerhandler ( FileSystemEventHandler):     @check_modification     def on_created (self, event):         pass     @check_modification     def on_ Deleted (self, event):        pass     @check_ Modification    def on_modified (self, event):         pass     @check_modification     def on_moved ( self, event):         passdef start_watching (Event_handler,  path= '. ',  recursive=true):    from watchdog.observers import  Observer    watcher = observer ()     watcher.schedule (Event_ handler=event_handler, path=path, recursive=recursive)     watcher.start ()      try:        while True:         &nbsP;   time.sleep (1)     except KeyboardInterrupt, e:         watcher.stop () if __name__ ==  ' __main__ ':     event_handler = customerhandler ()     start_watching (Event_handler=event_ Handler

Description: If you customize a class that inherits from FileSystemEventHandler, and implements the method of adding and deleting, all methods have an event parameter by default, and for the events object, in order to conveniently define a decorator directly, output the three common properties of the event object. Event.event_type, Event.is_directory, Event.src_path


Best Practices:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/89/84/wKioL1gWyAWQw5TVAAAwgsdfe8A995.png "title=" rs.png "alt=" Wkiol1gwyawqw5tvaaawgsdfe8a995.png "/>

1. People who have played flask know that in the debug mode, the modification of the py file will ever restart the whole program, to avoid the trouble of debugging manual restart, yesterday just received a demand, hope to write a simple plug-in system, support dynamic loading, we have until the program starts and then add/delete/check to the plugin directory Change the plug-in, the program is not aware of, in order to achieve similar flask overload effect, so that plug-in monitoring more intelligent, so learning the next flask source implementation, and their own handwritten a simplified version of the automatic heavy-duty decorator, directly see the source Bar ~

#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman#  oschina: http://xmdevops.blog.51cto.com/# purpose:# "" "#  Description:  Import public module Import osimport  sysimport timeimport threadingimport subprocess#  Description:  Import other modules #  instructions:  Base class monitoring Class basereloaderloop (object):     def __init__ (Self, watch_files,  interval):         self.watch_files = set ( Os.path.abspath (f)  for f in watch_files)          Self.interval = interval    def monitor (self):         pass    def rerun_with_autoreload (self):         while True:             envs = os.environ.copy ()              args = [sys.executable] +  sys.argv            envs.update ({' APP_ Autoreload ':  ' True '})             #  Description:   Blocked version of Popen            subprocess.call (args,  env=envs) #  description:  Default monitoring Class statreloaderloop (basereloaderloop):     def  __init__ (Self, *args, **kwargs):         super ( statreloaderloop, self). __init__ (*args, **kwargs) #  description: watchdogclass  Watchdogreloaderloop (Basereloaderloop):     def __init__ (self, *args, ** Kwargs):         super (watchdogreloaderloop, self). __init__ (*args,  **kwargs)       &Nbsp; self.scheduling_flag = true        from  watchdog.observers import observer        from  Watchdog.events import filesystemeventhandler        def  stop_scheduling ():             Self.scheduling_flag = false        class _eventhandler (FileSystemEventHandler):             def __ Init__ (self):                 Super (_eventhandler, self). __init__ ()              def on_any_event (self, event):                 stop_schedulinG ()         self.event_handler = _eventhandler ()          self.monitor_class = Observer    def  Monitor (self):         watcher = self.monitor_class ()          watcher.start ()         while  self.scheduling_flag:            for  path in self.watch_files:                 try:                     watcher.schedule (Event_handler=self.event_handler, path=path,  recursive=true)                  except  (OserroR, windowserror), e:                     #  Description:  Exception handling                      pass             time.sleep (Self.interval) reloader_loop = {      ' stat ': statreloaderloop,     ' watchdog ':  watchdogreloaderloop,}try:     __import__ (' watchdog.observers ') except importerror, e:     reloader_loop[' auto '] = reloader_loop[' status ']else:    reloader_loop[' Auto '] = reloader_loop[' watchdog ']#  description:  decorative function def run_with_autoreload (watch_files=none,  interval=1, rtype= ' auto '):     "" "decorator for run with  autoreloader.    :p Aram watch_files: file path    :type watch_files: list     :p aram interval: check interval    :type  interval: int    :p aram rtype: reload type    : Type rtype: str    :return: none    :rtype: none      "" "    def decorator (func):         def wrapper (*args, **kwargs):             reloader = reloader_loop[rtype] (watch_files, interval)              isreload = os.environ.get (' APP_ Autoreload ',  ' False ')             if  isreload ==  ' True ':    &NBSp;            cur_thread = threading. Thread (Target=func, args=args, kwargs=kwargs)                  cur_thread.setdaemon (True)                  cur_thread.start ()                  reloader.monitor ()              else:                 reloader.rerun_with_autoreload ()          return wrapper    return decorator

Description: The use of the method is very simple directly on your main program entry function @run_with_autoreload (...) Support settings to monitor multiple directories, set the monitoring interval, say the whole idea, first in the current environment to get the value of App_autoreload is True (the default is not configured and do not need to configure), if it is represented by the child process started, otherwise call Rerun_with_ Autoreload (), this method is mainly set environment variable app_autoreload Is true and uses Subprocess.call to invoke the command entered in the new environment Envs in the form of a child process, when the child process is executed because APP _autoreload is already true, so the main function we decorated is executed with a thread open, but note that it is set to Cur_thread.setdaemon (True), which means that once the child process is finished, It exits regardless of whether it is executed or not, while Reloader.monitor () acts as a blocking action for Cur_thread.join (), while the internal watchdog monitoring to any event modifies the value of Self.scheduling_flag, just Span style= "font-family: ' Andale mono '; font-size:10px;" >reloader.monitor () relies on this value for looping, so once the event is sent, the loop stops, and reloader.rerun_with_autoreload () causes the new subprocess to take over the entire application, This achieves the function of automatic overloading


Simple invocation:

Import timefrom wrappers.autoreload Import run_with_autoreload@run_with_autoreload (watch_files=['./img ', './css '), interval=1, rtype= ' Auto ') def main (): while true:print ' = + {} '. Format (Time.time ()) Time.sleep (1) if __n ame__ = = ' __main__ ': print ' found Notice:app start at {}. '. Format (Time.time ()) main ()

Description: The entry function of the program everyone according to their own application, such as just a simple demonstration, I am monitoring the current directory of the IMG/CSS directory, you can try to set up the Img/css directory in the current directory and then start the program and Add/delete/modify files in Img/css, Test that the entire application has no reload ~

This article is from the "@ Hubei @ white Hair" blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1867597

Basic Primer _python-modules and packages. What are the best practices for watchdog event monitoring in operational development?

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.