# -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license. 
# See the NOTICE for more information.

import os

import gevent
from gevent import core
from gevent import monkey
monkey.noisy = False
from gevent.pool import Pool
from gevent import wsgi

import gunicorn
from gunicorn.workers.base import Worker

class WSGIHandler(wsgi.WSGIHandler):
    def log_request(self, *args):
        pass

class GEvent2Worker(Worker):
    
    base_env = {
        'GATEWAY_INTERFACE': 'CGI/1.1',
        'SERVER_SOFTWARE': 'gevent/%s gunicorn/%s' % (gevent.__version__,
                                                    gunicorn.__version__),
        'SCRIPT_NAME': '',
        'wsgi.version': (1, 0),
        'wsgi.url_scheme': 'http',
        'wsgi.multithread': False,
        'wsgi.multiprocess': True,
        'wsgi.run_once': False
    }
    
    def __init__(self, *args, **kwargs):
        super(GEvent2Worker, self).__init__(*args, **kwargs)
        self.worker_connections = self.cfg.worker_connections
        self.pool = None
    
    @classmethod
    def setup(cls):
        from gevent import monkey
        monkey.patch_all(dns=False)
   
    def handle_request(self, req):
        self.pool.spawn(self.handle, req)
       
    def handle(self, req):
        handle = WSGIHandler(req)
        handle.handle(self)
        
    def run(self):
        self.socket.setblocking(1)
        env = self.base_env.copy()
        
        env.update({
            'SERVER_NAME': self.address[0],
            'SERVER_PORT': str(self.address[1]) 
        })
        self.base_env = env
        
        http = core.http()
        http.set_gencb(self.handle_request)
        self.pool = Pool(self.worker_connections)
        
        self.application = self.wsgi
        acceptor = gevent.spawn(http.accept, self.socket.fileno())
        
        try:
            while self.alive:
                self.notify()
            
                if self.ppid != os.getppid():
                    self.log.info("Parent changed, shutting down: %s" % self)
                    gevent.kill(acceptor)
                    break
                gevent.sleep(0.1)            
            self.pool.join(timeout=self.timeout)
        except KeyboardInterrupt:
            pass


        
        
        

@@ -266,16 +266,18 @@
  import 
+py
 wsgi%0A%0Aim
@@ -350,16 +350,18 @@
 Handler(
+py
 wsgi.WSG
@@ -423,42 +423,46 @@
 ss%0A%0A
+%0A
 class 
-GEvent2Worker(Work
+WSGIServer(pywsgi.WSGIServ
 er):%0A
-    %0A
     
@@ -473,25 +473,16 @@
 _env = %7B
-%0A        
 'GATEWAY
@@ -505,16 +505,24 @@
 I/1.1',%0A
+        
         
@@ -639,16 +639,24 @@
         
+        
 gunicorn
@@ -678,16 +678,24 @@
         
+    
+    
 'SCRIPT_
@@ -705,16 +705,24 @@
 E': '',%0A
+        
         
@@ -757,35 +757,8 @@
     
-'wsgi.url_scheme': 'http',%0A
     
@@ -788,32 +788,40 @@
  False,%0A        
+   
+     
 'wsgi.multiproce
@@ -829,14 +829,23 @@
 s': 
-True,%0A
+False,%0A        
     
@@ -874,14 +874,54 @@
 alse
+%7D%0A
 %0A    
-%7D
+    %0A    %0Aclass GEvent2Worker(Worker):%0A
 %0A   
@@ -1090,33 +1090,8 @@
 ons%0A
-        self.pool = None%0A
     
@@ -1205,442 +1205,186 @@
 %0A   
-%0A    def handle_request(self, req):%0A        self.pool.spawn(self.handle, req)%0A       %0A    def handle(self, req):%0A        handle = WSGIHandler(req)%0A        handle.handle(self)%0A        %0A    def run(self):%0A        self.socket.setblocking(1)%0A        env = self.base_env.copy()%0A        %0A        env.update(%7B%0A            'SERVER_NAME': self.address%5B0%5D,%0A            'SERVER_PORT': str(self.address%5B1%5D) %0A        %7D)%0A        self.base_env = env
+     %0A    def run(self):%0A        self.socket.setblocking(1)%0A        pool = Pool(self.worker_connections)%0A        %0A        server = WSGIServer(self.socket, application=self.wsgi, 
 %0A   
@@ -1388,25 +1388,24 @@
         
-%0A
         
 http = c
@@ -1400,232 +1400,116 @@
     
-http = core.http()%0A        http.set_gencb(self.handle_request)%0A        self.pool = Pool(self.worker_connections)%0A        %0A        self.application = self.wsgi%0A        acceptor = gevent.spawn(http.accept, self.socket.fileno()
+        spawn=pool, handler_class=WSGIHandler)%0A        %0A        acceptor = gevent.spawn(server.serve_forever
 )%0A  

rewrite ggevent2 workerr to support last gevent 0.13. a lot more stable and faster than it was. gevent 0.13 give new streaming possibilities and keepalive.