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

from __future__ import with_statement

import errno
import socket
import sys

import eventlet

from eventlet.green import os
from eventlet import greenlet
from eventlet import greenpool
from eventlet import greenthread
from eventlet import hubs

from gunicorn.workers.async import AsyncWorker
from gunicorn import util


class EventletWorker(AsyncWorker):

    @classmethod
    def setup(cls):
        import eventlet
        if eventlet.version_info < (0,9,7):
            raise RuntimeError("You need eventlet >= 0.9.7")
        eventlet.monkey_patch(os=False)
    
    def init_process(self):
        hubs.use_hub()
        super(EventletWorker, self).init_process()
        
    def timeout_ctx(self):
        return eventlet.Timeout(self.cfg.keepalive, False)
        
    def run(self):
        self.socket.setblocking(1)

        pool = greenpool.GreenPool(self.worker_connections)
        acceptor = greenthread.spawn(self.acceptor, pool)
        
        try:
            while self.alive:
                self.notify()
                
                if self.ppid != os.getppid():
                    self.log.info("Parent changed, shutting down: %s" % self)
                    break
            
                eventlet.sleep(self.timeout)            
        except KeyboardInterrupt:
            pass

        # we stopped
        greenthread.kill(acceptor, eventlet.StopServe)
        with eventlet.Timeout(self.timeout, False):
            if pool.waiting():
                pool.waitall()
       
        
    def acceptor(self, pool):
        acceptor_gt = greenthread.getcurrent()
        while self.alive:
            try:
                try:
                    conn, addr = self.socket.accept()
                except socket.error, e:
                    if e[0] == errno.EAGAIN:
                        sys.exc_clear()
                        return
                    raise
                gt = pool.spawn(self.handle, conn, addr)
                gt.link(self._stop_acceptor, acceptor_gt, conn)
                conn, addr, gt = None, None, None
            except eventlet.StopServe:
                return
            except:
                self.log.exception("Unexpected error in acceptor. Sepuku.")
                os._exit(4)


    def _stop_acceptor(self, t, acceptor_gt, conn):
        try:
            try:
                t.wait()
            finally:
                util.close(conn)
        except greenlet.GreenletExit:
            pass
        except Exception:
            greenthread.kill(acceptor_gt, *sys.exc_info())
        
<commit_msg>some fixes in eventlet worker<commit_after># -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license. 
# See the NOTICE for more information.

from __future__ import with_statement

import errno
import socket
import sys

import eventlet

from eventlet.green import os
from eventlet import greenlet
from eventlet import greenpool
from eventlet import greenthread
from eventlet import hubs

from gunicorn.workers.async import AsyncWorker
from gunicorn import util


class EventletWorker(AsyncWorker):

    @classmethod
    def setup(cls):
        import eventlet
        if eventlet.version_info < (0,9,7):
            raise RuntimeError("You need eventlet >= 0.9.7")
        eventlet.monkey_patch(os=False)
    
    def init_process(self):
        hubs.use_hub()
        super(EventletWorker, self).init_process()
        
    def timeout_ctx(self):
        return eventlet.Timeout(self.cfg.keepalive, False)
        
    def run(self):
        self.socket.setblocking(1)

        pool = greenpool.GreenPool(self.worker_connections)
        acceptor = eventlet.spawn(self.acceptor, pool)
        
        try:
            while self.alive:
                self.notify()
                
                if self.ppid != os.getppid():
                    self.log.info("Parent changed, shutting down: %s" % self)
                    break
            
                eventlet.sleep(self.timeout)            
        except KeyboardInterrupt:
            pass

        # we stopped
        eventlet.kill(acceptor, eventlet.StopServe)
        with eventlet.Timeout(self.timeout, False):
            if pool.waiting():
                pool.waitall()
       
        
    def acceptor(self, pool):
        acceptor_gt = greenthread.getcurrent()
        while self.alive:
            try:
                try:
                    conn, addr = self.socket.accept()
                except socket.error, e:
                    if e[0] == errno.EAGAIN:
                        sys.exc_clear()
                        return
                    raise
                gt = pool.spawn(self.handle, conn, addr)
                gt.link(self._stop_acceptor, acceptor_gt, conn)
                conn, addr, gt = None, None, None
            except socket.error, e:
                if e[0] not in (errno.EBADF, errno.EINVAL, errno.ENOTSOCK):
                    self.alive = False
                    break
            except eventlet.StopServe:
                break
            except:

                self.log.exception("Unexpected error in acceptor. Sepuku.")
                os._exit(4)

    def _stop_acceptor(self, t, acceptor_gt, conn):
        try:
            try:
                t.wait()
            finally:
                util.close(conn)
        except greenlet.GreenletExit:
            pass
        except Exception:
            greenthread.kill(acceptor_gt, *sys.exc_info())
        

{'fraction_non_alphanumeric': 0.0799645390070922, 'fraction_numerical': 0.003723404255319149, 'mean_word_length': 1.557116953762466, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6485537', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1495, 'n_words': 383}