<s> import errno <EOL> import os <EOL> import sys <EOL> from datetime import datetime <EOL> from functools import partial <EOL> import time <EOL> _socket = __import__ ( "<STR_LIT>" ) <EOL> if sys . platform == "<STR_LIT>" : <EOL> os . environ [ '<STR_LIT>' ] = "<STR_LIT:1>" <EOL> try : <EOL> import gevent <EOL> except ImportError : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> from gevent . pool import Pool <EOL> from gevent . server import StreamServer <EOL> from gevent . socket import wait_write , socket <EOL> from gevent import pywsgi <EOL> import gunicorn <EOL> from gunicorn . http . wsgi import base_environ <EOL> from gunicorn . workers . async import AsyncWorker <EOL> from gunicorn . http . wsgi import sendfile as o_sendfile <EOL> VERSION = "<STR_LIT>" % ( gevent . __version__ , gunicorn . __version__ ) <EOL> def _gevent_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> wait_write ( fdout ) <EOL> else : <EOL> raise <EOL> def patch_sendfile ( ) : <EOL> from gunicorn . http import wsgi <EOL> if o_sendfile is not None : <EOL> setattr ( wsgi , "<STR_LIT>" , _gevent_sendfile ) <EOL> class GeventWorker ( AsyncWorker ) : <EOL> server_class = None <EOL> wsgi_handler = None <EOL> def patch ( self ) : <EOL> from gevent import monkey <EOL> monkey . noisy = False <EOL> if gevent . version_info [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> monkey . patch_all ( ) <EOL> else : <EOL> monkey . patch_all ( subprocess = True ) <EOL> patch_sendfile ( ) <EOL> sockets = [ ] <EOL> for s in self . sockets : <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> sockets . append ( socket ( s . FAMILY , _socket . SOCK_STREAM , <EOL> fileno = s . sock . fileno ( ) ) ) <EOL> else : <EOL> sockets . append ( socket ( s . FAMILY , _socket . SOCK_STREAM , <EOL> _sock = s ) ) <EOL> self . sockets = sockets <EOL> def notify ( self ) : <EOL> super ( GeventWorker , self ) . notify ( ) <EOL> if self . ppid != os . getppid ( ) : <EOL> self . log . info ( "<STR_LIT>" , self ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def timeout_ctx ( self ) : <EOL> return gevent . Timeout ( self . cfg . keepalive , False ) <EOL> def run ( self ) : <EOL> servers = [ ] <EOL> ssl_args = { } <EOL> if self . cfg . is_ssl : <EOL> ssl_args = dict ( server_side = True , ** self . cfg . ssl_options ) <EOL> for s in self . sockets : <EOL> s . setblocking ( <NUM_LIT:1> ) <EOL> pool = Pool ( self . worker_connections ) <EOL> if self . server_class is not None : <EOL> environ = base_environ ( self . cfg ) <EOL> environ . update ( { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : VERSION , <EOL> } ) <EOL> server = self . server_class ( <EOL> s , application = self . wsgi , spawn = pool , log = self . log , <EOL> handler_class = self . wsgi_handler , environ = environ , <EOL> ** ssl_args ) <EOL> else : <EOL> hfun = partial ( self . handle , s ) <EOL> server = StreamServer ( s , handle = hfun , spawn = pool , ** ssl_args ) <EOL> server . start ( ) <EOL> servers . append ( server ) <EOL> try : <EOL> while self . alive : <EOL> self . notify ( ) <EOL> gevent . sleep ( <NUM_LIT:1.0> ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> except : <EOL> for server in servers : <EOL> try : <EOL> server . stop ( ) <EOL> except : <EOL> pass <EOL> raise <EOL> try : <EOL> for server in servers : <EOL> if hasattr ( server , '<STR_LIT>' ) : <EOL> server . close ( ) <EOL> if hasattr ( server , '<STR_LIT>' ) : <EOL> server . kill ( ) <EOL> ts = time . time ( ) <EOL> while time . time ( ) - ts <= self . cfg . graceful_timeout : <EOL> accepting = <NUM_LIT:0> <EOL> for server in servers : <EOL> if server . pool . free_count ( ) != server . pool . size : <EOL> accepting += <NUM_LIT:1> <EOL> if not accepting : <EOL> return <EOL> self . notify ( ) <EOL> gevent . sleep ( <NUM_LIT:1.0> ) <EOL> self . log . warning ( "<STR_LIT>" % self . pid ) <EOL> [ server . stop ( timeout = <NUM_LIT:1> ) for server in servers ] <EOL> except : <EOL> pass <EOL> def handle_request ( self , * args ) : <EOL> try : <EOL> super ( GeventWorker , self ) . handle_request ( * args ) <EOL> except gevent . GreenletExit : <EOL> pass <EOL> except SystemExit : <EOL> pass <EOL> def handle_quit ( self , sig , frame ) : <EOL> gevent . spawn ( super ( GeventWorker , self ) . handle_quit , sig , frame ) <EOL> if gevent . version_info [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> def init_process ( self ) : <EOL> self . patch ( ) <EOL> import gevent . core <EOL> gevent . core . reinit ( ) <EOL> gevent . core . dns_shutdown ( fail_requests = <NUM_LIT:1> ) <EOL> gevent . core . dns_init ( ) <EOL> super ( GeventWorker , self ) . init_process ( ) <EOL> else : <EOL> def init_process ( self ) : <EOL> self . patch ( ) <EOL> from gevent import hub <EOL> hub . reinit ( ) <EOL> super ( GeventWorker , self ) . init_process ( ) <EOL> class GeventResponse ( object ) : <EOL> status = None <EOL> headers = None <EOL> sent = None <EOL> def __init__ ( self , status , headers , clength ) : <EOL> self . status = status <EOL> self . headers = headers <EOL> self . sent = clength <EOL> class PyWSGIHandler ( pywsgi . WSGIHandler ) : <EOL> def log_request ( self ) : <EOL> start = datetime . fromtimestamp ( self . time_start ) <EOL> finish = datetime . fromtimestamp ( self . time_finish ) <EOL> response_time = finish - start <EOL> resp_headers = getattr ( self , '<STR_LIT>' , { } ) <EOL> resp = GeventResponse ( self . status , resp_headers , self . response_length ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> req_headers = [ h . split ( "<STR_LIT::>" , <NUM_LIT:1> ) for h in self . headers . headers ] <EOL> else : <EOL> req_headers = [ ] <EOL> self . server . log . access ( resp , req_headers , self . environ , response_time ) <EOL> def get_environ ( self ) : <EOL> env = super ( PyWSGIHandler , self ) . get_environ ( ) <EOL> env [ '<STR_LIT>' ] = self . socket <EOL> env [ '<STR_LIT>' ] = self . path <EOL> return env <EOL> class PyWSGIServer ( pywsgi . WSGIServer ) : <EOL> pass <EOL> class GeventPyWSGIWorker ( GeventWorker ) : <EOL> "<STR_LIT>" <EOL> server_class = PyWSGIServer <EOL> wsgi_handler = PyWSGIHandler </s>
