Actually, there are two ways to create a context manager, that ensures something to you. In my case it was connection to AMQP. The first way is to override concurrent.futures.Future
result()
method, forcing it to return a generator function decorated by contextlib.contextmanager
. ajdavis used this approach in his nice library TORO. You can see it by navigating to this line.
However, if you don't want to override concurrent.futures.Future
object, then I recommend you walk through the following snippet:
@gen.coroutine
def ensure_connection(*args, **kwargs):
res = yield _make_connection(timeout=kwargs.pop('timeout', 5), *args, **kwargs)
raise gen.Return(res)
@gen.coroutine
def ensure(*args, **kwargs):
res = yield ensure_connection(*args, **kwargs)
@contextlib.contextmanager
def func(res):
try:
yield # your wrapped code
finally:
pass
return func(res)
@gen.coroutine
def simple_usage(conn):
with (yield conn.ensure()) as res:
# Do your target staff here
conn = ...
IOLoop.add_callback(callback=lambda : simple_usage(conn))