您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“如何用python動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)模塊熱更新”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“如何用python動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)模塊熱更新”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
最近有個(gè)部署需求,需要讀取py文件格式的配置項(xiàng),我的實(shí)現(xiàn)思路是把配置文件解析到內(nèi)存中。主要使用兩種方法:
importlib.import_module
types.ModuleType
先來看看import module使用方法。
主要有兩個(gè)參數(shù):
package:包名
name:模塊名
返回 module 對(duì)象
現(xiàn)在開始實(shí)現(xiàn)動(dòng)態(tài)導(dǎo)包,成功讀取到配置項(xiàng)。
import importlib settings = importlib.import_module("remote_settings")
這樣子就能初步實(shí)現(xiàn)動(dòng)態(tài)倒入了,但是我有個(gè)需求,就是我的系統(tǒng)好些個(gè)模塊,用FOR循環(huán)導(dǎo)包,然后處理業(yè)務(wù)。然后問題來了,對(duì)同一個(gè)“包”導(dǎo)入多次,python并不會(huì)重新導(dǎo)入,而是返回內(nèi)存緩存中該模塊的地址。
下面驗(yàn)證一下,第一次寫入a = 123,第二次寫入a = "hello"。
輸出結(jié)果,兩次都是打印舊版本的變量,可見對(duì)同一個(gè)模塊進(jìn)行多次import_module,并不能實(shí)現(xiàn)熱更新。
必須要reload,模塊才會(huì)更新。
輸出結(jié)果如下,動(dòng)態(tài)reload后,成功獲得新版本a的值。
到此基本實(shí)現(xiàn)初步熱更新需求了,但是還有個(gè)問題:
問題一:重新加載的模塊不刪除舊版本在符號(hào)表中的登記項(xiàng),比如舊版本中存在變量a,新版本中刪除了該變量,但是重載不會(huì)更新該變化。
def load_module(module_name): module = importlib.import_module(module_name) return importlib.reload(module) def rewrite_file(file_name, content): with open(file_name, "w+") as f: f.write(content) def main(): rewrite_file(file_name, "a=123\nb=456") c1 = load_module(module_name) print(hasattr(c1, "a")) rewrite_file(file_name, "c=100\nd=200") c1 = load_module(module_name) print(hasattr(c1, "a"))
我們期望輸出 True、False,但是兩次都是輸出True,也就是說重新加載的模塊不會(huì)刪除最初舊版本模塊在符號(hào)表中的登記項(xiàng)。
手動(dòng)創(chuàng)建module對(duì)象,而不是使用內(nèi)存中的module對(duì)象。這種方法不需要判斷是否需要重載,而且是真正的更新,會(huì)刪除舊版本模塊的登記項(xiàng)。
import types def import_from_pyfile(filename): d = types.ModuleType("config") # 創(chuàng)建一個(gè)模塊對(duì)象 d.__file__ = filename try: with open(filename, "r") as config_file: exec(compile(config_file.read(), filename, "exec"), d.__dict__) except ImportError as e: print("failt to read config file: {}".format(filename)) raise e return d
下面驗(yàn)證一下
我們期望的輸出依次是True、False,符合需求
因此,這種方法能讓我們的模塊實(shí)現(xiàn)真正的重載。
無論是方法1還是方法2,都是返回一個(gè)module對(duì)象,module對(duì)象存在一些共性問題。
問題一:重新加載類不影響類的任何已存實(shí)例,已存實(shí)例將繼續(xù)使用原來的定義,只有重新加載后創(chuàng)建的新實(shí)例使用新定義。
# 原先的 Dog 定義 # class Dog(): # def __init__(self): # self.name = None c1 = load_module(module_name) old_dog = c1.Dog() # 中間去修改了 Dog 定義 # class Dog(): # def __init__(self): # self.name = "旺財(cái)" c1 = load_module(module_name) new_dog = c1.Dog() print(old_dog.name, new_dog.name) >>> ouput: None 旺財(cái)
問題二:模塊內(nèi)的引用,不會(huì)被reload。比如模塊configA中引用了其他模塊(configB),當(dāng)configB發(fā)生變化,重新加載configA,并不會(huì)對(duì)configB進(jìn)行重載。
預(yù)期應(yīng)該依次輸出 configB version1、configBversion2,但是輸出了兩次configB version1,這說明了模塊內(nèi)的引用,不會(huì)被reload,需要手動(dòng)更新它。
我這實(shí)現(xiàn)了一個(gè)遞歸更新方法,不僅對(duì)當(dāng)前模塊熱更新,還更新里面所有的引用。
def load_module(module): if isinstance(module, str): # 首次import module = importlib.import_module(module) return importlib.reload(module) def reload_module(module): load_module(module) for key, child_module in vars(module).items(): if isinstance(child_module, types.ModuleType): reload_module(child_module)
效果如下:
def test_reload_module(): configA = "config" configB = "./configB.py" configC = "./configC.py" rewrite_file(configB, "import configC\nname ='configB version1'") rewrite_file(configC, "name ='configC version1'") confA = load_module(configA) print("原始configB.name:", confA.configB.name) print("原始configC.name:", confA.configB.configC.name) a = 123 rewrite_file(configB, "import configC\nname ='configB version2'") rewrite_file(configC, "name ='configC version2'") confA = load_module(configA) print("非遞歸重載configA, configB.name:", confA.configB.name) print("非遞歸重載configA, configC.name:", confA.configB.configC.name) reload_module(confA) print("遞歸重載configA, configB.name:", confA.configB.name) print("遞歸重載configA, configC.name:", confA.configB.configC.name)
日志如下:
讀到這里,這篇“如何用python動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)模塊熱更新”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。