<s> from functools import partial <EOL> import errno <EOL> import sys <EOL> try : <EOL> import eventlet <EOL> except ImportError : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if eventlet . version_info < ( <NUM_LIT:0> , <NUM_LIT:9> , <NUM_LIT:7> ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> from eventlet import hubs , greenthread <EOL> from eventlet . greenio import GreenSocket <EOL> from eventlet . hubs import trampoline <EOL> import greenlet <EOL> from gunicorn . http . wsgi import sendfile as o_sendfile <EOL> from gunicorn . workers . async import AsyncWorker <EOL> def _eventlet_sendfile ( fdout , fdin , offset , nbytes ) : <EOL> while True : <EOL> try : <EOL> return o_sendfile ( fdout , fdin , offset , nbytes ) <EOL> except OSError as e : <EOL> if e . args [ <NUM_LIT:0> ] == errno . EAGAIN : <EOL> trampoline ( fdout , write = True ) <EOL> else : <EOL> raise <EOL> def _eventlet_serve ( sock , handle , concurrency ) : <EOL> """<STR_LIT>""" <EOL> pool = eventlet . greenpool . GreenPool ( concurrency ) <EOL> server_gt = eventlet . greenthread . getcurrent ( ) <EOL> while True : <EOL> try : <EOL> conn , addr = sock . accept ( ) <EOL> gt = pool . spawn ( handle , conn , addr ) <EOL> gt . link ( _eventlet_stop , server_gt , conn ) <EOL> conn , addr , gt = None , None , None <EOL> except eventlet . StopServe : <EOL> pool . waitall ( ) <EOL> return <EOL> def _eventlet_stop ( client , server , conn ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> client . wait ( ) <EOL> finally : <EOL> conn . close ( ) <EOL> except greenlet . GreenletExit : <EOL> pass <EOL> except Exception : <EOL> greenthread . kill ( server , * sys . exc_info ( ) ) <EOL> def patch_sendfile ( ) : <EOL> from gunicorn . http import wsgi <EOL> if o_sendfile is not None : <EOL> setattr ( wsgi , "<STR_LIT>" , _eventlet_sendfile ) <EOL> class EventletWorker ( AsyncWorker ) : <EOL> def patch ( self ) : <EOL> hubs . use_hub ( ) <EOL> eventlet . monkey_patch ( os = False ) <EOL> patch_sendfile ( ) <EOL> def init_process ( self ) : <EOL> self . patch ( ) <EOL> super ( EventletWorker , self ) . init_process ( ) <EOL> def timeout_ctx ( self ) : <EOL> return eventlet . Timeout ( self . cfg . keepalive or None , False ) <EOL> def handle ( self , listener , client , addr ) : <EOL> if self . cfg . is_ssl : <EOL> client = eventlet . wrap_ssl ( client , server_side = True , <EOL> ** self . cfg . ssl_options ) <EOL> super ( EventletWorker , self ) . handle ( listener , client , addr ) <EOL> def run ( self ) : <EOL> acceptors = [ ] <EOL> for sock in self . sockets : <EOL> gsock = GreenSocket ( sock ) <EOL> gsock . setblocking ( <NUM_LIT:1> ) <EOL> hfun = partial ( self . handle , gsock ) <EOL> acceptor = eventlet . spawn ( _eventlet_serve , gsock , hfun , <EOL> self . worker_connections ) <EOL> acceptors . append ( acceptor ) <EOL> eventlet . sleep ( <NUM_LIT:0.0> ) <EOL> while self . alive : <EOL> self . notify ( ) <EOL> try : <EOL> eventlet . sleep ( <NUM_LIT:1.0> ) <EOL> except AssertionError : <EOL> self . alive = False <EOL> break <EOL> self . notify ( ) <EOL> try : <EOL> with eventlet . Timeout ( self . cfg . graceful_timeout ) as t : <EOL> [ a . kill ( eventlet . StopServe ( ) ) for a in acceptors ] <EOL> [ a . wait ( ) for a in acceptors ] <EOL> except eventlet . Timeout as te : <EOL> if te != t : <EOL> raise <EOL> [ a . kill ( ) for a in acceptors ] </s>
