Search code examples
pythondjangomod-fcgid

Reload mod_fcgid without killing Python Service


I'm currently running a Django project on my school's webserver with FCGI. I did follow the multiple guides that recommends installing a virtual local Python environment and it worked out great. The only issue i had was that "touching" my fcgi-file to reload source-files wasn't enough, but instead i had to kill the python service via SSH. This because mod_fcgid is used.

However, the admin didn't think it was a great idea that i ran my own local python. He thought it better if i just told him what modules to install on root, which was a pretty nice service really.

But doing this, i can no longer kill python since it's under root(though immoral as I am, I've definitely tried). The admins recommendation was that I should try too make the fcgi script reload itself by checking time stamp. I've tried to find documentation on how to do this, but fund very little and since I'm a absolute beginner i have no idea what would work. Anyone have experience running python/django under mod_fcgid or tips on where to find related guides/documentation?


Solution

  • here's what I would do:

    ## top of my .fcgi script
    import sys, time
    original_modules = sys.modules.copy()
    
    ## in a separate thread
    old_ctime = os.path.getctime("mymodule.py")
    while True:
        time.sleep(10)
        new_ctime = os.path.getctime("mymodule.py")
        if new_ctime > old_ctime:
            sys.modules = original_modules # reset all imports
            import mymodule
            mymodule.dofcgi()
    

    granted this isn't drop-in perfect (you might have to mess w/ the threading) but it should give you a general idea of how to "reload" a module completely.