<s> from datetime import datetime <EOL> import errno <EOL> import socket <EOL> import ssl <EOL> import sys <EOL> import gunicorn . http as http <EOL> import gunicorn . http . wsgi as wsgi <EOL> import gunicorn . util as util <EOL> import gunicorn . workers . base as base <EOL> from gunicorn import six <EOL> ALREADY_HANDLED = object ( ) <EOL> class AsyncWorker ( base . Worker ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AsyncWorker , self ) . __init__ ( * args , ** kwargs ) <EOL> self . worker_connections = self . cfg . worker_connections <EOL> def timeout_ctx ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def handle ( self , listener , client , addr ) : <EOL> req = None <EOL> try : <EOL> parser = http . RequestParser ( self . cfg , client ) <EOL> try : <EOL> listener_name = listener . getsockname ( ) <EOL> if not self . cfg . keepalive : <EOL> req = six . next ( parser ) <EOL> self . handle_request ( listener_name , req , client , addr ) <EOL> else : <EOL> proxy_protocol_info = { } <EOL> while True : <EOL> req = None <EOL> with self . timeout_ctx ( ) : <EOL> req = six . next ( parser ) <EOL> if not req : <EOL> break <EOL> if req . proxy_protocol_info : <EOL> proxy_protocol_info = req . proxy_protocol_info <EOL> else : <EOL> req . proxy_protocol_info = proxy_protocol_info <EOL> self . handle_request ( listener_name , req , client , addr ) <EOL> except http . errors . NoMoreData as e : <EOL> self . log . debug ( "<STR_LIT>" , e ) <EOL> except StopIteration as e : <EOL> self . log . debug ( "<STR_LIT>" , e ) <EOL> except ssl . SSLError : <EOL> six . reraise ( * sys . exc_info ( ) ) <EOL> except EnvironmentError : <EOL> six . reraise ( * sys . exc_info ( ) ) <EOL> except Exception as e : <EOL> self . handle_error ( req , client , addr , e ) <EOL> except ssl . SSLError as e : <EOL> if e . args [ <NUM_LIT:0> ] == ssl . SSL_ERROR_EOF : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> client . close ( ) <EOL> else : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> self . handle_error ( req , client , addr , e ) <EOL> except EnvironmentError as e : <EOL> if e . errno not in ( errno . EPIPE , errno . ECONNRESET ) : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> else : <EOL> if e . errno == errno . ECONNRESET : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> else : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> except Exception as e : <EOL> self . handle_error ( req , client , addr , e ) <EOL> finally : <EOL> util . close ( client ) <EOL> def handle_request ( self , listener_name , req , sock , addr ) : <EOL> request_start = datetime . now ( ) <EOL> environ = { } <EOL> resp = None <EOL> try : <EOL> self . cfg . pre_request ( self , req ) <EOL> resp , environ = wsgi . create ( req , sock , addr , <EOL> listener_name , self . cfg ) <EOL> environ [ "<STR_LIT>" ] = True <EOL> self . nr += <NUM_LIT:1> <EOL> if self . alive and self . nr >= self . max_requests : <EOL> self . log . info ( "<STR_LIT>" ) <EOL> resp . force_close ( ) <EOL> self . alive = False <EOL> if not self . cfg . keepalive : <EOL> resp . force_close ( ) <EOL> respiter = self . wsgi ( environ , resp . start_response ) <EOL> if respiter == ALREADY_HANDLED : <EOL> return False <EOL> try : <EOL> if isinstance ( respiter , environ [ '<STR_LIT>' ] ) : <EOL> resp . write_file ( respiter ) <EOL> else : <EOL> for item in respiter : <EOL> resp . write ( item ) <EOL> resp . close ( ) <EOL> request_time = datetime . now ( ) - request_start <EOL> self . log . access ( resp , req , environ , request_time ) <EOL> finally : <EOL> if hasattr ( respiter , "<STR_LIT>" ) : <EOL> respiter . close ( ) <EOL> if resp . should_close ( ) : <EOL> raise StopIteration ( ) <EOL> except StopIteration : <EOL> raise <EOL> except EnvironmentError : <EOL> six . reraise ( * sys . exc_info ( ) ) <EOL> except Exception : <EOL> if resp and resp . headers_sent : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> try : <EOL> sock . shutdown ( socket . SHUT_RDWR ) <EOL> sock . close ( ) <EOL> except EnvironmentError : <EOL> pass <EOL> raise StopIteration ( ) <EOL> raise <EOL> finally : <EOL> try : <EOL> self . cfg . post_request ( self , req , environ , resp ) <EOL> except Exception : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> return True </s>
