mod_wsgi force reload modules

后端 未结 4 913
醉梦人生
醉梦人生 2021-02-04 07:32

Is there a way to have mod_wsgi reload all modules (maybe in a particular directory) on each load?

While working on the code, it\'s very annoying to restart apache every

4条回答
  •  抹茶落季
    2021-02-04 07:56

    I know it's an old thread but this might help someone. To kill your process when any file in a certain directory is written to, you can use something like this:

    monitor.py

    import os, sys, time, signal, threading, atexit
    import inotify.adapters
    
    def _monitor(path):
    
        i = inotify.adapters.InotifyTree(path)
    
        print "monitoring", path
        while 1:
            for event in i.event_gen():
                if event is not None:
                    (header, type_names, watch_path, filename) = event
                    if 'IN_CLOSE_WRITE' in type_names:
                        prefix = 'monitor (pid=%d):' % os.getpid()
                        print "%s %s/%s changed," % (prefix, path, filename), 'restarting!'
                        os.kill(os.getpid(), signal.SIGKILL)
    
    def start(path):
    
        t = threading.Thread(target = _monitor, args = (path,))
        t.setDaemon(True)
        t.start()
    
        print 'Started change monitor. (pid=%d)' % os.getpid()
    

    In your server startup, call it like:

    server.py

    import monitor
    
    monitor.start()
    

    if your main server file is in the directory which contains all your files, you can go like:

    monitor.start(os.path.dirname(__file__))
    

    Adding other folders is left as an exercise...

    You'll need to 'pip install inotify'

    This was cribbed from the code here: https://code.google.com/archive/p/modwsgi/wikis/ReloadingSourceCode.wiki#Restarting_Daemon_Processes

    This is an answer to my duplicate question here: WSGI process reload modules

提交回复
热议问题