def stream_with_context(generator_or_function):
 try:
  gen = iter(generator_or_function)
 except TypeError:
  def decorator(*args, **kwargs):
   gen = generator_or_function()
   return stream_with_context(gen)
  return update_wrapper(decorator, generator_or_function)
 def generator():
  ctx = _request_ctx_stack.top
  if (ctx is None):
   raise RuntimeError('Attempted to stream with context but there was no context in the first place to keep around.')
  with ctx:
   (yield None)
   try:
    for item in gen:
     (yield item)
   finally:
    if hasattr(gen, 'close'):
     gen.close()
 wrapped_g = generator()
 next(wrapped_g)
 return wrapped_g

def stream_with_context ( generator_or_function ) : try : gen = iter ( generator_or_function ) except TypeError : def decorator ( * args , ** kwargs ) : gen = generator_or_function ( ) return stream_with_context ( gen ) return update_wrapper ( decorator , generator_or_function ) def generator ( ) : ctx = _request_ctx_stack . top if ( ctx is None ) : raise RuntimeError ( 'Attempted to stream with context but there was no context in the first place to keep around.' ) with ctx : ( yield None ) try : for item in gen : ( yield item ) finally : if hasattr ( gen , 'close' ) : gen . close ( ) wrapped_g = generator ( ) next ( wrapped_g ) return wrapped_g
request contexts disappear when the response is started on the server .