第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我是否需要在 Python 中為資源包裝器實(shí)現(xiàn) Dispose 模式

我是否需要在 Python 中為資源包裝器實(shí)現(xiàn) Dispose 模式

慕工程0101907 2021-08-05 17:16:27
如果我要在 Python 中實(shí)現(xiàn)一個安全的資源包裝器,我是否需要像 C# 一樣實(shí)現(xiàn)Dispose 模式?這是我的意思的演示實(shí)現(xiàn):class ResourceWrapper:    def __init__(self):        self._python_resource = ...  # A Python object that manages some resources.        self._external_resource = _allocate_resource()  # A resource handle to an external resource.        self._is_closed = False  # Whether the object has been closed.    def __del__(self):        self._close(manual_close=False)  # Called by GC.    def close(self):        self._close(manual_close=True)  # Called by user to free resource early.    def _close(self, manual_close):        if not self._is_closed:  # Don’t want a resource to be closed more than once.            if manual_close:                # Since `_close` is called by user, we can guarantee that `self._python_resource` is still valid, so we                # can close it safely.                self._python_resource.close()             else:                # This means `_close` is called by GC, `self._python_resource` might be already GCed, but we don’t know                # for sure, so we do nothing and rely on GC to free `self._python_resource`.                pass            # GC will not take care of freeing unmanaged resource, so whether manual close or not, we have to close the            # resource to prevent leaking.            _free_resource(self._external_resource)            # Now we mark the object as closed to prevent closing multiple times.            self._is_closed = Trueself._python_resource是一個由 Python GC 管理的資源包裝器對象,self._external_resource是一個不受 Python GC 管理的外部資源的句柄。如果用戶手冊關(guān)閉包裝器,我想確保托管和非托管資源都被釋放,如果包裝器對象被 GC,它們也會被釋放。
查看完整描述

1 回答

?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗 獲得超17個贊

不,在 Python 中你應(yīng)該使用Context Managers:


class ResourceWrapper:

    def __init__(self):

        ...


    ...



    def __enter__(self):

        return self


    def __exit__(self, type, value, traceback):

        self._close(manual_close=False)


with ResourceWrapper() as wrapper:

    # do something with wrapper

注1:方法中有這樣的注釋_close():


這意味著_close被 GC 調(diào)用,self._python_resource可能已經(jīng)被 GC,但我們不確定,所以我們什么都不做,依靠 GC 來釋放self._python_resource.


我不確定您的意思,但只要您持有對對象的引用(并且只要它不是弱引用),它就不會被 GC 處理。


注 2:如果一個上下文管理器的對象在沒有with阻塞的情況下使用會發(fā)生什么?然后當(dāng)對象被垃圾收集時資源將被釋放 - 但我不會擔(dān)心。使用上下文管理器是 Python 中的常見習(xí)慣用法(請參閱任何帶有open()ing 文件的示例)。如果這對您的應(yīng)用程序至關(guān)重要,您可以在 中獲取資源__enter__(),除非在with塊中否則不會獲取資源。


注意 3,關(guān)于循環(huán)引用:如果你有兩個對象相互持有引用,你就形成了循環(huán)引用,這樣兩個對象就不會被“常規(guī)”引用計數(shù) GC 釋放。相反,它們將由分代 GC 收集,除非他們碰巧有__del__方法。__del__禁止 GC 收集對象。見gc.garbage:


收集器發(fā)現(xiàn)無法訪問但無法釋放的對象列表(不可收集的對象)。默認(rèn)情況下,此列表僅包含帶有__del__() methods. [1] 具有__del__()方法并且是引用循環(huán)的一部分的對象 會導(dǎo)致整個引用循環(huán)不可收集,包括不一定在循環(huán)中但只能從循環(huán)中訪問的對象。


Python 3.4 引入了PEP-442,它引入了安全對象終結(jié)。無論哪種方式,您都不會有無效的引用。如果您有屬性 ( hasattr(self, "_python_resource")),它將是有效的。


查看完整回答
反對 回復(fù) 2021-08-05
  • 1 回答
  • 0 關(guān)注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號