Search code examples
pythoncherrypygunicorn

Restart Python.py when it stops working


I am using Cherrypy framework to run my python code on server. But the process stops working when the load increases.

Every time this happens I have to manually go and start the python code. Is there any way i can use Gunicorn with Cherrypy so that Gunicorn can start the code automatically when it stops working.

Any other solution will also work in this case. Just want to make sure that the python program does not stop working.


Solution

  • I use a cron that checks the memory load every few minutes and resets cherrypy when the memory exceeds 500MB -- so that the web host doesn't complain to me with emails. Something on my server doesn't release memory when a function ends as it should, so this is a pragmatic work around.

    This hack may be weird because I reset it using an HTTP request, but that's because I spent hours trying to figure out how to do this withing the BASH and gave up. It works.

    CRON PART

    */2 * * * * /usr/local/bin/python2.7 /home/{mypath}/cron_reset_cp.py > $HOME/cron.log 2>&1
    

    And code inside cron_reset_cp.py...

    #cron for resetting cherrypy /cp/ when 500+ MB
    import os
    #assuming starts in /home/my_username/
    os.chdir('/home/my_username/cp/')
    import mem
    C = mem.MemoryMonitor('my_username') #this function adds up all the memory
    memory = int(float(C.usage()))
    if memory > 500:#MB
        #### Tried: pid = os.getpid() #current process = cronjob --- THIS approach did not work for me.
        import urllib2
        cp = urllib2.urlopen('http://myserver.com/cp?reset={password}')
    

    Then I added this function to reset the cherrypy via cron OR after a github update from any browser (assuming only I know the {password})

    The reset url would be http://myserver.com/cp?reset={password}

    def index(self, **kw):
        if kw.get('reset') == '{password}': 
            cherrypy.engine.restart()
            ip = cherrypy.request.headers["X-Forwarded-For"] #get_client_ip
            return 'CherryPy RESETTING for duty, sir! requested by '+str(ip)
    

    The MemoryMonitor part is from here: How to get current CPU and RAM usage in Python?