mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
fix issue #348 .
Rather than testing the parent pd, test if the parent pid is still alive. Only use it in gevent for now.
This commit is contained in:
parent
c1b073a7ac
commit
da637dfd13
@ -0,0 +1,22 @@
|
|||||||
|
from multiprocessing import Process, Queue
|
||||||
|
import requests
|
||||||
|
|
||||||
|
def child_process(queue):
|
||||||
|
while True:
|
||||||
|
print queue.get()
|
||||||
|
r = requests.get('http://friendpaste.com')
|
||||||
|
print r.headers
|
||||||
|
|
||||||
|
class GunicornSubProcessTestMiddleware(object):
|
||||||
|
def __init__(self):
|
||||||
|
super(GunicornSubProcessTestMiddleware, self).__init__()
|
||||||
|
self.queue = Queue()
|
||||||
|
self.process = Process(target=child_process, args=(self.queue,))
|
||||||
|
self.process.start()
|
||||||
|
|
||||||
|
def process_request(self, request):
|
||||||
|
self.queue.put(('REQUEST',))
|
||||||
|
|
||||||
|
def process_response(self, request, response):
|
||||||
|
self.queue.put(('RESPONSE',response.status_code))
|
||||||
|
return response
|
||||||
@ -98,6 +98,7 @@ MIDDLEWARE_CLASSES = (
|
|||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
# Uncomment the next line for simple clickjacking protection:
|
# Uncomment the next line for simple clickjacking protection:
|
||||||
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
|
'testing.apps.someapp.middleware.GunicornSubProcessTestMiddleware'
|
||||||
)
|
)
|
||||||
|
|
||||||
ROOT_URLCONF = 'testing.urls'
|
ROOT_URLCONF = 'testing.urls'
|
||||||
|
|||||||
@ -353,3 +353,10 @@ def check_is_writeable(path):
|
|||||||
except IOError, e:
|
except IOError, e:
|
||||||
raise RuntimeError("Error: '%s' isn't writable [%r]" % (path, e))
|
raise RuntimeError("Error: '%s' isn't writable [%r]" % (path, e))
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
def is_process_running(process_id):
|
||||||
|
try:
|
||||||
|
os.kill(process_id, 0)
|
||||||
|
return True
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
|||||||
@ -23,6 +23,7 @@ from gevent import pywsgi
|
|||||||
|
|
||||||
import gunicorn
|
import gunicorn
|
||||||
from gunicorn.workers.async import AsyncWorker
|
from gunicorn.workers.async import AsyncWorker
|
||||||
|
from gunicorn.util import is_process_running
|
||||||
|
|
||||||
VERSION = "gevent/%s gunicorn/%s" % (gevent.__version__, gunicorn.__version__)
|
VERSION = "gevent/%s gunicorn/%s" % (gevent.__version__, gunicorn.__version__)
|
||||||
|
|
||||||
@ -36,6 +37,7 @@ BASE_WSGI_ENV = {
|
|||||||
'wsgi.run_once': False
|
'wsgi.run_once': False
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class GeventWorker(AsyncWorker):
|
class GeventWorker(AsyncWorker):
|
||||||
|
|
||||||
server_class = None
|
server_class = None
|
||||||
@ -61,11 +63,12 @@ class GeventWorker(AsyncWorker):
|
|||||||
else:
|
else:
|
||||||
server = StreamServer(self.socket, handle=self.handle, spawn=pool)
|
server = StreamServer(self.socket, handle=self.handle, spawn=pool)
|
||||||
|
|
||||||
server.start()
|
gevent.spawn(server.start)
|
||||||
try:
|
try:
|
||||||
while self.alive:
|
while self.alive:
|
||||||
self.notify()
|
self.notify()
|
||||||
if self.ppid != os.getppid():
|
|
||||||
|
if not is_process_running(self.ppid):
|
||||||
self.log.info("Parent changed, shutting down: %s", self)
|
self.log.info("Parent changed, shutting down: %s", self)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user