<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> import os <EOL> import thread <EOL> import threading <EOL> from google . appengine . api . logservice import logservice <EOL> from google . appengine . runtime import request_environment <EOL> _original_start_new_thread = thread . start_new_thread <EOL> _ENVIRON_KEYS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> _original_environ = dict ( os . environ ) <EOL> _filtered_environ = { } <EOL> def _capture_environ ( ) : <EOL> global _filtered_environ <EOL> _filtered_environ = dict ( ( k , v ) <EOL> for k , v in os . environ . iteritems ( ) <EOL> if k in _ENVIRON_KEYS ) <EOL> _filtered_environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class FrontendsNotSupported ( Error ) : <EOL> """<STR_LIT>""" <EOL> class BackgroundThreadLimitReachedError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def start_new_background_thread ( target , args , kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> _capture_environ ( ) <EOL> _original_start_new_thread ( _bootstrap , ( target , args , kwargs ) ) <EOL> class BackgroundThread ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> _capture_environ ( ) <EOL> if not self . _Thread__initialized : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if self . _Thread__started . is_set ( ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> with threading . _active_limbo_lock : <EOL> threading . _limbo [ self ] = self <EOL> try : <EOL> start_new_background_thread ( self . _Thread__bootstrap , ( ) ) <EOL> except Exception : <EOL> with threading . _active_limbo_lock : <EOL> del threading . _limbo [ self ] <EOL> raise <EOL> self . _Thread__started . wait ( ) <EOL> def _bootstrap ( target , args , kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> if kwargs is None : <EOL> kwargs = { } <EOL> errors = logservice . LogsBuffer ( ) <EOL> environ = dict ( _original_environ ) <EOL> if _filtered_environ : <EOL> environ . update ( _filtered_environ ) <EOL> try : <EOL> request_environment . current_request . Init ( errors , environ ) <EOL> target ( * args , ** kwargs ) <EOL> finally : <EOL> request_environment . current_request . Reset ( ) </s>
