python 热更可以通过官方库importlib.reload()
reload后,模块使用新代码,新new对象也会使用新代码。
但是reload前new的对象依旧用着旧代码,甚至缺少更新后的属性。
pyhot可以解决这个问题
安装 pip install pyhot
例子:
test.py
import pyhot.pyhot as pyhot
class TestObj_1(pyhot.HotObj):
obj_id = 0
new_obj_id = 0
new_obj_id_1 = 1
def __init__(self, obj_id=0):
super(TestObj_1, self).__init__()
self.obj_id = obj_id
def test_fun(self):
print("class:%s, obj_id:%s, var:%s, new_obj_id_4:%s"%(self.__class__, self.obj_id, 2, self.new_obj_id_1))
main.py
import pyhot.pyhot as pyhot
import test
a = test.TestObj_1(2)
while True:
a.test_fun()
in_str = input("输入:\n")
if in_str == "1":
pyhot.hot_mgr.hot("test")
else:
pass
在test.py对class做修改后,输入1触发pyhot热更,就可以看到打印变化
效果如下
可以看到函数变化了,新的属性也会加上
还有更方便的方法
import pyhot.pyhot as pyhot
import test_1.test as test
a = test.TestObj(1)
# 初始化需要热更的目录
pyhot.hot_mgr.init_file_record()
while True:
a.test_fun()
b.test_fun()
c.test_fun()
in_str = input("输入:\n")
if in_str == "1":
# 自动热更变化的文件
pyhot.hot_mgr.hot_all()
else:
pass
init_file_record 会初始化项目下的文件信息
hot_all 会自动检查修改的文件,并热更,不需要手动填需要更新的模块名
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行