目录


语法[TOC]

with <context_manager_object> [as <variable>]:
    with-block

说明[TOC]

上下文管理器,本质上是使用with语句来简化try/finally模式。它可以保证,即使在出错的情况下,也可以执行 某些诸如释放数据库连接等 清理语句。
实现了__enter__(self)和__exit__(self, exc_type, exc_value, traceback)这两个方法的对象,就支持上下文管理。当进入with语句块时,会执行上下文管理器对象的__enter__方法,并且把__enter__方法的返回值作为as子句后面的变量的值;当离开with语句块的时候,会执行上下文管理器对象的__exit__方法,并把with语句块中抛出的异常信息作为__exit__方法的参数。


例子[TOC]

[root@iZj6chejzrsqpclb7miryaZ ~]# cat test.py 
import time

class TimeUsed:
    def __init__(self):
        self._start_time = None
        self._end_time = None

    def __enter__(self):
        self._start_time = time.time()
    
    def __exit__(self, exc_type, exc_value, traceback):
        self._end_time = time.time()
        print "time used: %ss" % (self._end_time - self._start_time) 

if __name__ == "__main__":
    def func(n):
        time.sleep(n)

    with TimeUsed():
        func(2)

    with TimeUsed():
        func(1)

[root@iZj6chejzrsqpclb7miryaZ ~]# python test.py 
time used: 2.00210905075s
time used: 1.00110411644s

contextmanager装饰器[TOC]

为了简化上下文管理器的编写,Python标准库的contextlib模块提供了contextmanager装饰器,它可以把生成器函数包装成支持上下文管理的对象。
当进入with语句块的时候,会调用生成器对象的next方法,并把yield传递出的值,作为as子句后面的变量的值;当离开with语句块的时候,会调用生成器对象的next或throw方法(当with语句块出异常时,会调用生成器对象的throw方法,此时,可以在生成器中对异常进行处理,不做任何异常处理的话,会重新抛出异常)。

[root@iZj6chejzrsqpclb7miryaZ ~]# cat test.py 
from contextlib import contextmanager
import time

@contextmanager
def time_used():
    start = time.time()

    try:
        yield
    finally:
        end = time.time()
        print "time used: %s" % (end - start)

if __name__ == "__main__":
    def func(n, will_raise=False):
        time.sleep(n)

        if will_raise:
            raise RuntimeError("in func")

    with time_used():
        func(2)

    with time_used():
        func(1, True)

[root@iZj6chejzrsqpclb7miryaZ ~]# python test.py 
time used: 2.00210595131
time used: 1.00112104416
Traceback (most recent call last):
  File "test.py", line 25, in 
    func(1, True)
  File "test.py", line 19, in func
    raise RuntimeError("in func")
RuntimeError: in func