python 热更库 pyhot

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行
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值