After the two "run-time View thread Information" blog, I was thinking that since I could print thread information at any time, I would be able to print other information inside the process at any time. For example, viewing some object properties in real time can help us observe the execution state of a process without restarting the application. (The use of Third-party libraries or tools is not considered for the time being)
According to this idea, looked at Python dynamic loading module method, feel that the idea is still more reliable, should be able to achieve, so start writing a small test validated a. (here to illustrate, only confirmatory, production environment to use, there are still a lot of problems to consider. )
Here's what to think about when testing.
-or use the sigquit signal, "kill-3", to trigger the execution of the internal object properties of the print process.
-foo.py main program, including registration signal processing function, create a global object to save some properties, start a thread to let the main thread not exit.
-foo.py the signal processing function in the main program dynamically loads a module under a specified path, where we assume that the module path is "/tmp/my_modules" and can be modified as needed. Then call the method in this module to print some process information.
-/tmp/my_modules/bar.py requires dynamically loaded modules, which access an object in the Foo module and print object properties.
-to dynamically modify the process state to view at any time, that is, to modify the content to be viewed by modifying the bar.py file without restarting the process. Main program foo.py
#!/usr/bin/env/usr/bin/python3.4 #-*-coding:utf-8-*-import sys import threading import sign
Al from datetime import datetime Import Time Class MyObject (object): Def __init__ (self): Self.data = {}
Self.data[' a '] = ' aaa ' self.data[' B '] = ' bbb ' self.data[' c '] = ' CCC ' def Test (): While True:
Print (DateTime.Now ()) Time.sleep (2) # Signal processing function def signal_handler (Signum, frame): Try: # dynamic Loading Module Sys.path.append ("/tmp/my_modules") # import bar Module bar = __import__ (' bar ') reload module so that you can reload the mold at any time Block Reload (BAR) # calls a method that dynamically loads a module bar.execute () except baseexception as E:print (e) My_ob ject = MyObject () if __name__ = = "__main__": try:signal.signal (signal. Sigquit, Signal_handler) threading. Thread (Target=test). Start () while True:time.sleep except KeyboardInterrupt:sys.exit (1)
modules that need to be loaded dynamically/tmp/my_modules/bar.py
#!/usr/bin/env/usr/bin/python3.4
#-*-coding:utf-8-*-
import foo
def execute ():
# Print Objects
in the Foo module print ' my_object:%s '% Foo.my_object.data
Test
Run foo.py First
$ python foo.py
Then find the foo.py process number, and then use "kill-3" to trigger the method of printing the Memory object
$ kill-3 <pid>
You should see the properties of the foo.py process print my_object at this point.
Modify the/tmp/my_modules/bar.py file and then run "$ kill-3" again to see that the module is reloaded and the new content is printed.