Python对象对重新执行程序的持久性 [英] Python object persistence over re-execs

查看:68
本文介绍了Python对象对重新执行程序的持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过运行脚本的重新执行持久化对象的方法在哪里?如果我希望正在运行的脚本重新执行自身以获取任何代码更改,(os.exec *)是否可以在重新执行后持久保存对象以进行访问?我可以使用腌制的ascii数据设置环境变量,或将数据写入管道并在重新执行后重新读取它,但这看起来不雅或像黑客一样.即使这样做,也不是所有物品都能腌制得很好.

Is where a way to persist objects over re-execs of a running script? If I want a running script to re execute itself to pick up any code changes, (os.exec*) is there a way to persist the objects for access after the re-execution? I could set environment variables with pickled ascii data, or write that data to a pipe and re-read it after the re-execution, but that seems inelegant or like a hack. Even if doing that, not all items pickle well.

推荐答案

如果将代码放在模块中,则可以使用

If you put your code in a module, you can use the reload() standard function to load the new version of the code. Your main module could look like this:

import mymodule

while mymodule.go():
    reload(mymodule)

每当要重新加载模块代码时,请从go()返回True.要退出时,返回False.

Whenever you want to reload your module code, return True from go(). When you want to exit, return False.

这篇关于Python对象对重新执行程序的持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆