深入了解​Python上下文管理器模塊--contextlib

2021-03-02 Python學習開發

在 Python 處理文件的時候我們使用 with 關鍵詞來進行文件的資源關閉,但是並不是只有文件操作才能使用 with 語句。今天就讓我們一起學習 Python 中的上下文管理 contextlib。

上下文管理器

上下文,簡而言之,就是程式所執行的環境狀態,或者說程式運行的情景。既然提及上下文,就不可避免的涉及 Python 中關於上下文的魔法。上下文管理器(context manager)是 Python2.5 開始支持的一種語法,用於規定某個對象的使用範圍。一旦進入或者離開該使用範圍,會有特殊操作被調用。它的語法形式是 with…as…,主要應用場景資源的創建和釋放。例如,文件就支持上下文管理器,可以確保完成文件讀寫後關閉文件句柄。

with open('password.txt', 'wt') as f:
    f.write('contents go here')

__enter__和__exit__

with 方法的實現涉及到兩個魔法函數__enter__和__exit__。
執行流進入 with 中的代碼塊時會執行__enter__方法,它會返回在這個上下文中使用的一個對象。執行流離開 with 塊時,則調用這個上下文管理器的__exit__方法來清理所使用的資源。

class Context:
    def __init__(self):
        print("int __init__")

    def __enter__(self):
        print("int __enter__")

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("in __exit__")

if __name__ == '__main__':
    with Context():
        print('start with ')

輸出

int __init__
int __enter__
start witht
in __exit__

相對於使用 try:finally 塊,使用 with 語句代碼看起來更緊湊,with 代碼塊執行的時候總會調用__exit__方法,即使出現了異常。
如果給 with 語句的 as 子句指定一個別名,那麼__enter__方法可以返回與這個名關聯的任何對象。

import requests


class Request:
    def __init__(self):
        self.session = requests.session()

    def get(self, url, headers=None):
        if headers is None:
            headers = {}

        response = self.session.get(url)
        return response


class Context:
    def __init__(self):
        print("int __init__")

    def __enter__(self):
        print("int __enter__")
        return Request()

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("in __exit__")


if __name__ == '__main__':
    with Context() as t:
        req = t.get("https://wwww.baidu.com")
        print(req.text)

這裡的 t 就是__enter__方法返回的 Request 對象的實例,然後我們可以調用該實例的一些方法。
如果上下文中出現異常,可以通過修改__exit__方法,如果返回值為 true,則可以把異常列印出來,如果為 false 則會拋出異常。

class Context:
    def __init__(self,flag):
        self.flag = flag

    def __enter__(self):
        print("int __enter__")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("in __exit__")
        print(f"{exc_type=}") 
        print(f"{exc_val=}")
        print(f"{exc_val=}")
        return self.flag


if __name__ == '__main__':
    with Context(True) as t:
        raise RuntimeError()

    print("-華麗的分割線----")

    with Context(False) as t:
        raise RuntimeError()

輸出

int __enter__
in __exit__
exc_type=<class 'RuntimeError'>
exc_val=RuntimeError()
exc_val=RuntimeError()

-華麗的分割線----

int __enter__
in __exit__
exc_type=<class 'RuntimeError'>
exc_val=RuntimeError()
exc_val=RuntimeError()
Traceback (most recent call last):
  File "demo.py", line 26, in <module>
    raise RuntimeError()
RuntimeError

可以看到__exit__方法接收一些參數,其中包含 with 塊中產生的異常的詳細信息。

上下文管理器作為裝飾器

通過繼承 contextlib 裡面的 ContextDecorator 類,實現對常規上下文管理器類的支持,其不僅可以作為上下文管理器,也可以作為函數修飾符。

import contextlib


class Context(contextlib.ContextDecorator):

    def __init__(self, how_used):
        self.how_used = how_used
        print(f'__init__({how_used})')

    def __enter__(self):
        print(f'__enter__({self.how_used})')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print(f'__exit__({self.how_used})')


@Context('這是裝飾器方式')
def func(message):
    print(message)
print("")
func('作為裝飾器運行')


print("\n--華麗的分割線---\n")
with Context('上下文管理器方式'):
    print('emmmm')

看一下 ContextDecorator 的源碼就了解了

class ContextDecorator(object):
    "A base class or mixin that enables context managers to work as decorators."

    def _recreate_cm(self):
        """Return a recreated instance of self.

        Allows an otherwise one-shot context manager like
        _GeneratorContextManager to support use as
        a decorator via implicit recreation.

        This is a private interface just for _GeneratorContextManager.
        See issue #11647 for details.
        """
        return self

    def __call__(self, func): 
        @wraps(func)
        def inner(*args, **kwds):
            with self._recreate_cm():
                return func(*args, **kwds)
        return inner

生成器函數轉為上下文管理器

有時候我們的代碼只有很少的上下文要管理,此時再使用上面的形式寫出 with 相關的魔法函數
就顯得比較囉嗦了,在這種情況下,我們可以使用 contextmanager 修飾符將一個生成器轉換為上下文管理器。

import contextlib

@contextlib.contextmanager
def make_context():
    print("enter make_context")
    try:
        yield {}
    except RuntimeError as err:
        print(f"{err=}")

print("Normal")
with make_context() as value:
    print("in with")

print("RuntimeError")
with make_context() as value:
    raise RuntimeError("runtimeerror")

print("Else Error")
with make_context() as value:
    raise ZeroDivisionError("0 不能作為分母")

輸出結果

enter make_context
in with
existing
RuntimeError:
enter make_context
err=RuntimeError(
existing
Else Error:
enter make_context
existing
Traceback (most recent call last):
  File "demo.py", line 24, in <module>
    raise ZeroDivisionError("0 不能作為除數")
ZeroDivisionError: 0 不能作為除數

需要主要的是這個函數必須是個裝飾器
被裝飾器裝飾的函數分為三部分:
with 語句中的代碼塊執行前執行函數中 yield 之前代碼
yield 返回的內容複製給 as 之後的變量
with 代碼塊執行完畢後執行函數中 yield 之後的代碼

再簡單理解就是
yield 前半段用來表示__enter__()
yield 後半段用來表示__exit__()

在這裡,我們從 contextlib 模塊中引入 contextmanager,然後裝飾我們所定義的 make_context 函數。這就允許我們使用 Python 的 with 語句來調用 make_context 函數。在函數中通過 yield 一個空字典,將其傳遞出去,最終主調函數可以使用它。

一旦 with 語句結束,控制就會返回給 make_context 函數,它繼續執行 yield 語句後面的代碼,這個最終會執行 finally 語句列印 existing。如果我們遇到了 RuntimeError 錯誤,它就會被捕獲,最終 finally 語句依然會列印 existing。

因為 contextmanager 繼承自 ContextDecorator,所以也可以被用作函數修飾符

import contextlib
@contextlib.contextmanager
def make_context():
    print("enter make_context")
    try:
        yield {}
    except RuntimeError as err:
        print(f"{err=}")
    finally:
         print("existing")
@make_context()
def normal():
    print("in normal")
@make_context()
def raise_error(err):
   raise err
if __name__ == '__main__':
    print("Normal:")
    normal()
    print("RuntimeError:")
    raise_error(RuntimeError("runtime error"))
    print("Else Error")
    raise_error(ValueError("value error"))

輸出

Normal:
enter make_context
in normal
existing
RuntimeError:
enter make_context
err=RuntimeError('runtime error')
existing
Else Error
enter make_context
existing
Traceback (most recent call last):
  File "demo.py", line 33, in <module>
    raise_error(ValueError("value error"))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "demo.py", line 21, in raise_error
    raise err
ValueError: value error

這是 contextmanager 原理:
1、因為 func()已經是個生成器了嘛,所以運行enter_()的時候,contextmanager 調用 self.gen.next()會跑到 func 的 yield 處,停住掛起,這個時候已經有了 t1=time.time()2、然後運行 with 語句體裡面的語句,也就是 a+b=3003、跑完後運行exit()的時候,contextmanager 調用 self.gen.next()會從 func 的 yield 的下一句開始一直到結束。這個時候有了 t2=time.time(),t2-t1 從而實現了統計 cost_time 的效果,完美。
源碼

class GeneratorContextManager(object):
    """Helper for @contextmanager decorator."""

    def __init__(self, gen):
        self.gen = gen

    def __enter__(self):
        try:
            return self.gen.next()
        except StopIteration:
            raise RuntimeError("generator didn't yield")

    def __exit__(self, type, value, traceback):
        if type is None:
            try:
                self.gen.next()
            except StopIteration:
                return
            else:
                raise RuntimeError("generator didn't stop")
        else:
            if value is None:
                
                
                value = type()
            try:
                self.gen.throw(type, value, traceback)
                raise RuntimeError("generator didn't stop after throw()")
            except StopIteration, exc:
                return exc is not value
            except:
                if sys.exc_info()[1] is not value:
                    raise


def contextmanager(func):
    @wraps(func)
    def helper(*args, **kwds):
        return GeneratorContextManager(func(*args, **kwds))
    return helper

關閉打開的句柄

並不是所有的類都支持上下文管理器的 API,有一些遺留的類會使用一個 close 方法。
為了確保關閉句柄,需要使用 closing 為他創建一個上文管理器。
該模塊主要是解決不能支持上下文管理器的類。

import contextlib


class Http():
    def __init__(self):
        print("int init:")
        self.session = "open"

    def close(self):
        """
        關閉的方法必須叫 close
        """
        print("in close:")
        self.session = "close"


if __name__ == '__main__':
    with contextlib.closing(Http()) as http:
        print(f"inside session value:{http.session}")
    print(f"outside session value:{http.session}")

    with contextlib.closing(Http()) as http:
        print(f"inside session value:{http.session}")
        raise EnvironmentError("EnvironmentError")
    print(f"outside session value:{http.session}")

輸出

int init:
inside session value:open
in close:
outside session value:close
int init:
inside session value:open
in close:
Traceback (most recent call last):
  File "demo.py", line 23, in <module>
    raise EnvironmentError("EnvironmentError")
OSError: EnvironmentError

可以看到即使程序出現了錯誤,最後也會執行 close 方法的內容。
看一眼下面的源碼就知道 closing 幹嘛了

class closing(object):
    """Context to automatically close something at the end of a block.
    Code like this:
        with closing(<module>.open(<arguments>)) as f:
            <block>
    is equivalent to this:
        f = <module>.open(<arguments>)
        try:
            <block>
        finally:
            f.close()
    """
    def __init__(self, thing):
        self.thing = thing
    def __enter__(self):
        return self.thing
    def __exit__(self, *exc_info):
       self.thing.close()

這個 contextlib.closing()會幫它加上__enter__()和__exit__(),使其滿足 with 的條件。然後 exit 裡執行的就是對應類的 close 方法。

巧妙的迴避錯誤

contextlib.suppress(*exceptions)
另一個工具就是在 Python 3.4 中加入的 suppress 類。這個上下文管理工具背後的理念就是它可以禁止任意數目的異常。假如我們想忽略 FileNotFoundError 異常。如果你書寫了如下的上下文管理器,那麼它不會正常運行。

>>> with open("1.txt") as fobj:
...     for line in fobj:
...         print(line)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '1.txt'

正如你所看到的,這個上下文管理器沒有處理這個異常,如果你想忽略這個錯誤,你可以按照如下方式來做,

>>> from contextlib import suppress
>>> with suppress(FileNotFoundError):
...     with open("1.txt") as fobj:
...         for line in fobj:
...             print(line)

在這段代碼中,我們引入 suppress,然後將我們要忽略的異常傳遞給它,在這個例子中,就是 FileNotFoundError。如果你想運行這段代碼,你將會注意到,文件不存在時,什麼事情都沒有發生,也沒有錯誤被拋出。請注意,這個上下文管理器是可重用的。

例子資料庫的自動提交和回滾

在編程中如果頻繁的修改資料庫, 一味的使用類似 try:… except..: rollback() raise e 其實是不太好的.

   try:
        gift = Gift()
        gift.isbn = isbn
        ... 
        db.session.add(gift)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        raise e

為了達到使用 with 語句的目的, 我們可以重寫 db 所屬的類:

from flask_sqlalchemy import SQLAlchemy as _SQLALchemy
class SQLAlchemy(_SQLALchemy):
    @contextmanager
    def auto_commit(self):
        try:
            yield
            self.session.commit()
        except Exception as e:
            db.session.rollback()
            raise e

這時候, 在執行數據的修改的時候便可以:

 with db.auto_commit():
        gift = Gift()
        gift.isbn = isbndb.session.add(gift)
        db.session.add(gift)

with db.auto_commit():
    user = User()
    user.set_attrs(form.data)
    db.session.add(user)

上下文管理器很有趣,也很方便。我經常在自動測試中使用它們,例如,打開和關閉對話。現在,你應該可以使用 Python 內置的工具去創建你的上下文管理器。你還可以繼續閱讀 Python 關於 contextlib 的文檔,那裡有很多本文沒有覆蓋到的知識。

這篇文章其實主要為了後面的異步上文管理器使用的。

參考資料

https://www.cnblogs.com/zhbzz2007/p/6158125.html
https://blog.csdn.net/emaste_r/article/details/78105713
https://blog.csdn.net/weixin_42359464/article/details/80742387#three
https://docs.python.org/zh-cn/3/library/contextlib.html?highlight=contextlib#module-contextlib

點擊閱讀原文獲取Python 爬蟲面試題 170 道:2019 版

相關焦點

  • 有趣的Python上下文管理器
    另一個途徑是通過導入contextlib.contextmanager庫來實現。該庫使用生成器創建上下文管理器。方法如下所示: 還可以通過contextlib.contextmanager創建功能相同的裝飾器。
  • Python的with語句與上下文管理器詳解
    二、上下文管理器原理f 對象之所以會自動執行自己的close方法,是因為它是一個上下文管理器,所以我們要先說說什麼是上下文管理器。下面我們通過contextmanager裝飾器也實現一個關於文件的上下文管理器:from contextlib import contextmanager@contextmanagerdef open_file(filename, mode):    print('進入')    f = open(filename, mode)
  • 一文弄懂Python上下文管理器和with用法
    python中叫上下文管理器。本文帶你快速入門上下文管理器和with用法。上下文管理器,英文context managers,在python官方文檔中這樣描述:上下文管理器是一個對象,它定義了在執行 with 語句時要建立的運行時上下文。 上下文管理器處理進入和退出所需運行時上下文以執行代碼塊。 通常使用 with 語句(在 with 語句中描述),但是也可以通過直接調用它們的方法來使用。
  • python異常處理與上下文管理器
    # python 2 中except exception_type, error# python 3 中except exception_type as errortry: do somethingexcept exception_type1: when get exception_type1 errorexcept exception_type2
  • Python--- 上下文管理器
    (context manager)是Python2.5開始支持的一種語法,用於規定某個對象的使用範圍。我們的第二段程序就使用了上下文管理器 (with...as...)。上下文管理器有隸屬於它的程序塊。當隸屬的程序塊執行結束的時候(也就是不再縮進),上下文管理器自動關閉了文件 (我們通過f.closed來查詢文件是否關閉)。我們相當於使用縮進規定了文件對象f的使用範圍。
  • Python 中的 With 語句
    decimal模塊中 的新 localcontext() 函數使保存和還原當前decimal上下文變得容易,它封裝了計算所需的精度和捨入特徵:                from decimal import Decimal, Context, localcontext# 顯示默認精度:28 位數字
  • Python小知識:淺談Python的with語句
    已經加入對上下文管理協議支持的還有模塊 threading、decimal 等。PEP 0343 對 with 語句的實現進行了描述。with 語句的執行過程類似如下代碼塊:context_manager = context_expressionexit = type(context_manager).
  • 5年 Python 功力,總結了 10 個開發技巧
    __context__屬性,這也能在 traceback 更好的顯示異常信息。/lib/python3.7/lib-dynload','/home/wangbm/.local/lib/python3.7/site-packages','/usr/local/Python3.7/lib/python3.7/site-packages']>>>那有沒有更快的方式呢?
  • 來,一起體驗下Python的Pathlib模塊~
    '))在本教程中,你將了解如何使用pathlib模塊操作目錄和文件的名稱。使用pathlib模塊,可以使代碼使用更優雅,可讀和Pythonic代碼重寫上面的兩個示例,如:>>> path.parent>>> (pathlib.Path.home() / 'realpython.txt').is_file()Python文件路徑處理問題
  • 淺談Python中的with語句
    本文對 with 語句的語法和工作機理進行了介紹,並通過示例介紹了如何實現自定義的上下文管理器,最後介紹了如何使用 contextlib 模塊來簡化上下文管理器的編寫。with 語句是從 Python 2.5 開始引入的一種與異常處理相關的功能,從 2.6 版本開始預設可用。
  • 了解這些操作,Python中99%的文件操作都將變得遊刃有餘!
    Python中含有幾個用於執行文件操作的內置模塊,例如讀取文件,移動文件,獲取文件屬性等。本文總結了許多值得了解的函數,這些函數可用於進行一些Python中最常見的文件操作,可以極大地提高我們處理文件的效率。打開&關閉文件讀取或寫入文件前,首先要做的就是打開文件,Python的內置函數open可以打開文件並返回文件對象。
  • 叮咚,我精心幫你總結了Python with上下文管理器的知識點,請查收!
    正好前幾天有小夥伴在「測試開發群」裡問「Python上下文管理器」有哪些使用場景。我感覺多數人應該經常用,但是換了個問法後就有點陌生了,所以我花點時間給大家整理下Python上下文管理器的知識點。中,讀寫文件我們經常用下面這種寫法:with open("test_file.txt", "w+") as test_file:  print(test_file)  test_file.write("hello world")這裡其實就用到了with上下文管理器
  • 第38天:Python decimal 模塊
    在我們開發工作中浮點類型的使用還是比較普遍的,對於一些涉及資金金額的計算更是不能有絲毫誤差,Python 的 decimal  模塊為浮點型精確計算提供了支持。1 簡介 decimal 模塊設計以十進位數、算術上下文和信號這三個概念為中心。
  • 8個例子掌握Python的Pathlib模塊
    引言Python 的 pathlib 模塊使處理文件路徑變得非常簡單和高效。os.path模塊也可以用於處理路徑名操作。不同之處在於path模塊創建表示文件路徑的字符串,而pathlib創建路徑對象。使用路徑對象而不是字符串的一個重要優點是,我們可以在路徑對象上調用方法。
  • python進程池Pool的apply與apply_async到底怎麼用?
    多進程python中使用multiprocessing模塊實現多進程。multiprocessing模塊提供了一個Process類來代表一個進程對象,這個模塊表示像線程一樣管理進程,是multiprocessing的核心,它與threading很相似,對多核CPU的利用率會比threading好的多。
  • 讓你的 Python 代碼優雅又地道
    分離臨時上下文# 保存舊的,創建新的old_context = getcontext().copy()getcontext().prec = 50print Decimal(355) / Decimal(113)setcontext(old_context)更好的方法with localcontext
  • 深入剖析with的內部原理-異常處理的神器
    3).with的代碼執行完了,f就會被自動關閉有同學肯定忍不住的問,你還是沒有解釋為啥with語句能自動關閉啊,好我們來剖析一下:3.深入剖析with內部原理,4步搞定step1:1).with 語句看起來簡單,其實是因為有一個上下文管理器
  • Python自定義異常類、with上下文管理與trackback模塊
    看看執行效果:【2】with上下文管理在學習異常的結構後,我們得知finally塊中的代碼不管異常是否發生都會被執行,執行完之後就釋放try塊中申請的資源。Python提供來另外一種釋放資源的方法,那就是通過with上下文管理來實現。該方式的優點在於:它可以自動管理資源,在 with 代碼塊執行完畢後自動還原進入該代碼之前的現場或上下文。
  • 【Python基礎】Python十大文件騷操作!!
    顯示當前目錄當我們想知道當前的工作目錄是什麼的時候,我們可以簡單地使用os模塊的getcwd()功能,或者使用pathlib的cwd(),如下所示。Traceback (most recent call last):  File "<input>", line 3, in <module>  File "/Users/ycui1/.conda/envs/Medium/lib/python3.8/pathlib.py", line 1284, in mkdir    self.