check parents if we are in the worker. fix #348

This commit is contained in:
benoitc 2012-06-27 08:08:04 +02:00
parent 326356a18c
commit cdd3e1dc2b
3 changed files with 27 additions and 1 deletions

View File

@ -0,0 +1,22 @@
from multiprocessing import Process, Queue
import requests
import gevent
def child_process(queue):
while True:
print queue.get()
requests.get('http://requestb.in/15s95oz1')
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

View File

@ -98,6 +98,8 @@ MIDDLEWARE_CLASSES = (
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
# uncomment the next line to test multiprocessing
#'testing.apps.someapp.middleware.GunicornSubProcessTestMiddleware',
)
ROOT_URLCONF = 'testing.urls'

View File

@ -62,10 +62,12 @@ class GeventWorker(AsyncWorker):
server = StreamServer(self.socket, handle=self.handle, spawn=pool)
server.start()
pid = os.getpid()
try:
while self.alive:
self.notify()
if self.ppid != os.getppid():
if pid == os.getpid() and self.ppid != os.getppid():
self.log.info("Parent changed, shutting down: %s", self)
break