How to expire (and revive) workers in gunicorn?

孤街醉人 提交于 2019-12-10 05:58:11

问题


I have an application with a slow memory leak which, for various reasons, I can't get rid of. So I'd like to use the old trick of having my workers periodically die and revive.

(i.e. use the same strategy as maxtasksperchild in multiprocessing Pool ... "...A frequent pattern found in other systems (such as Apache, mod_wsgi, etc) to free resources held by workers is to allow a worker within a pool to complete only a set amount of work before being exiting, being cleaned up and a new process spawned to replace the old one...")

So far the best I've been able to come up with is to have a thread that sleeps, then calls os._exit(-1).

Is that the way to go, or is there a better way to periodically recycle my workers?

Here's the path I'm going down at the moment:

class Quitter(Thread):

    def run(self):
        while True:
            time.sleep(random.randrange(5, 7)):
            print str(os.getpid())
            os._exit(-1)

Quitter().start()

To which gunicorn responds with:

2013-03-13 03:21:24 [6487] [INFO] Booting worker with pid: 6487
...
2013-03-13 03:21:30 [6492] [INFO] Booting worker with pid: 6487

回答1:


Gunicorn actually has this available as a configuration option - take a look at max_requests.



来源:https://stackoverflow.com/questions/15397472/how-to-expire-and-revive-workers-in-gunicorn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!