def CallWithRetryAsync(retry_policy, func, *args, **kwargs):
 inner_callback = kwargs.get('callback', None)
 assert ('callback' in kwargs), 'CallWithRetryAsync requires a named "callback" argument that is not None.'
 retry_manager = retry_policy.CreateManager()
 def _OnCompletedCall(*callback_args, **callback_kwargs):
  'Called when the operation has completed. Determine whether to retry,\n    based on the arguments to the callback.\n    '
  retry_func = functools.partial(func, *args, **kwargs)
  if (not retry_manager.MaybeRetryOnResult(retry_func, *callback_args, **callback_kwargs)):
   exception_context.check_retry = False
   inner_callback(*callback_args, **callback_kwargs)
 def _OnException(type, value, tb):
  'Called if the operation raises an exception. Determine whether to retry\n    or re-raise the exception, based on the exception details.\n    '
  if exception_context.check_retry:
   retry_func = functools.partial(func, *args, **kwargs)
   return retry_manager.MaybeRetryOnException(retry_func, type, value, tb)
 kwargs['callback'] = _OnCompletedCall
 exception_context = ExceptionStackContext(_OnException)
 exception_context.check_retry = True
 with exception_context:
  func(*args, **kwargs)

def CallWithRetryAsync ( retry_policy , func , * args , ** kwargs ) : inner_callback = kwargs . get ( 'callback' , None ) assert ( 'callback' in kwargs ) , 'CallWithRetryAsync requires a named "callback" argument that is not None.' retry_manager = retry_policy . CreateManager ( ) def _OnCompletedCall ( * callback_args , ** callback_kwargs ) : retry_func = functools . partial ( func , * args , ** kwargs ) if ( not retry_manager . MaybeRetryOnResult ( retry_func , * callback_args , ** callback_kwargs ) ) : exception_context . check_retry = False inner_callback ( * callback_args , ** callback_kwargs ) def _OnException ( type , value , tb ) : if exception_context . check_retry : retry_func = functools . partial ( func , * args , ** kwargs ) return retry_manager . MaybeRetryOnException ( retry_func , type , value , tb ) kwargs [ 'callback' ] = _OnCompletedCall exception_context = ExceptionStackContext ( _OnException ) exception_context . check_retry = True with exception_context : func ( * args , ** kwargs )
this is a higher-order function that wraps an arbitrary asynchronous function in order to add retry functionality .