python结束程序再重新执行_Python对象对重新执行程序的持久性

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)

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


版权声明:本文为weixin_29196891原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。