#NEWLINE# def Handle( environ ) : #NEWLINE# #INDENT# 'Handles a background request.\n\n  This function is called by the runtime in response to an /_ah/background\n  request.\n\n  Args:\n    environ: A dict containing the environ for this request (i.e. for\n        os.environ).\n\n  Returns:\n    A dict containing:\n      error: App Engine error code. 0 if completed successfully, 1 otherwise.\n      response_code: The HTTP response code. 200 if completed successfully, 500\n          otherwise.\n      logs: A list of tuples (timestamp_usec, severity, message) of log\n          entries.  timestamp_usec is a long, severity is int and message is\n          str.  Severity levels are 0..4 for Debug, Info, Warning, Error,\n          Critical.\n  ' #NEWLINE# error = logservice . LogsBuffer ( ) #NEWLINE# request_environment . current_request . Init ( error , environ ) #NEWLINE# response = { #NEWLINE# #INDENT# 'error' : 0 , #NEWLINE# 'response_code' : 200 , #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# request_id = environ [ BACKGROUND_REQUEST_ID ] #NEWLINE# _pending_background_threads . RunBackgroundThread ( request_id ) #NEWLINE# return response #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# exception = sys . exc_info ( ) #NEWLINE# tb = exception [ 2 ] . tb_next #NEWLINE# if tb : #NEWLINE# #INDENT# tb = tb . tb_next #NEWLINE# #UNINDENT# message = '' . join ( traceback . format_exception ( exception [ 0 ] , exception [ 1 ] , tb ) ) #NEWLINE# logging . error ( message ) #NEWLINE# response [ 'response_code' ] = 500 #NEWLINE# response [ 'error' ] = 1 #NEWLINE# return response #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# request_environment . current_request . Clear ( ) #NEWLINE# response [ 'logs' ] = error . parse_logs ( )