Python with语句及context manager

with语句是对context manager(上下文管理者,以下简称CM)方法的包装,适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源,比如文件使用后自动关闭、线程中锁的自动获取和释放等。

with_item ::=  expression ["as" target]
    with-body
with open(‘x.txt’, mode=’w+’) as f:
f.write(‘xxxx\r\n’)

expression必须返回一个CM,CM的两个方法如下:

object.__enter__(self) 
object.__exit__(self, exc_type, exc_value, traceback) 

enter会在获取CM后调用,而exit会被存储并在with-body运行完成或者抛出异常后调用。

class withclass:
    def __init__(self):
        self.d = dict()

    def __enter__(self):
        print('do enter!')

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('cleanup the resources.')
        return True

def with_t():
    with withclass() as wc:
        print('before exception!')
        raise Exception('A error occur!')
        print('after exception!')
    print('after with statement!')

if __name__ == '__main__':
    with_t()

输出为:

do enter!
before exception!
cleanup the resources.
after with statement!

可以看到抛出的异常被无视了,而资源也被成功释放;原因是抛出异常后进入exit代码并返回了True,而如果返回了False则会继续抛出产生的异常,现在我们修改exit的返回为False。

def __exit__(self, exc_type, exc_val, exc_tb):
        print('cleanup the resources.')
        return False

再看结果:

Traceback (most recent call last):
do enter!
  File "E:/QQSyncFolder/prj/mysite/pattern/with.py", line 30, in <module>
before exception!
    with_t()
cleanup the resources.
  File "E:/QQSyncFolder/prj/mysite/pattern/with.py", line 25, in with_t
    raise Exception('A error occur!')
Exception: A error occur!

可以发现,资源仍然被清除,但是Exception却被抛出来,可以被外层程序捕获处理。

总的而言,主要是为了简化try…except…finally的使用,同时使用with也避免了忘记在finally中释放资源的情况。
像threading,mutliprocessing中Lock,Condition,Semaphore等多线程,多进程通讯都实现了context manager接口。

def RLock:    
    __enter__ = acquire
    def __exit__(self, t, v, tb):
        self.release()

# 下面的语法多开心:)

with rlock_instance:
    # do sth

PS: contextlib module 提供了许多简单易用的context装饰器,请参考Python官方文档。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章