runtime.py
的内容。换句话说,您不必中断runner.py
。#runner.py
import time
import imp
def main():
while True:
mod = imp.load_source("runtime", "./runtime.py")
mod.function()
time.sleep(1)
if __name__ == "__main__":
main()
在运行时导入的模块是:
# runtime.py
def function():
print("I am version one of runtime.py")
这种基本机制允许您像Erlang一样实现Python代码的“热交换”。有没有更好的替代方案?
请注意,这只是一个学术性问题,因为我不需要做任何类似的事情。然而,我对了解Python运行时很感兴趣。
编辑:
我创建了以下解决方案:一个Engine
对象提供了对模块中包含的函数的接口(在本例中,该模块名为engine.py
)。Engine
对象还生成一个线程来监视源文件中的更改,如果检测到更改,则调用引擎上的notify()
方法重新加载源文件。
在我的实现中,更改检测基于每frequency
秒轮询一次文件的SHA1校验和,但也可以使用其他实现方式。
在此示例中,检测到的每个更改都将记录到名为hotswap.log
的文件中,其中注册了校验和。
检测更改的其他机制可能是服务器或Monitor
线程中使用inotify
。
import imp
import time
import hashlib
import threading
import logging
logger = logging.getLogger("")
class MonitorThread(threading.Thread):
def __init__(self, engine, frequency=1):
super(MonitorThread, self).__init__()
self.engine = engine
self.frequency = frequency
# daemonize the thread so that it ends with the master program
self.daemon = True
def run(self):
while True:
with open(self.engine.source, "rb") as fp:
fingerprint = hashlib.sha1(fp.read()).hexdigest()
if not fingerprint == self.engine.fingerprint:
self.engine.notify(fingerprint)
time.sleep(self.frequency)
class Engine(object):
def __init__(self, source):
# store the path to the engine source
self.source = source
# load the module for the first time and create a fingerprint
# for the file
self.mod = imp.load_source("source", self.source)
with open(self.source, "rb") as fp:
self.fingerprint = hashlib.sha1(fp.read()).hexdigest()
# turn on monitoring thread
monitor = MonitorThread(self)
monitor.start()
def notify(self, fingerprint):
logger.info("received notification of fingerprint change ({0})".\
format(fingerprint))
self.fingerprint = fingerprint
self.mod = imp.load_source("source", self.source)
def __getattr__(self, attr):
return getattr(self.mod, attr)
def main():
logging.basicConfig(level=logging.INFO,
filename="hotswap.log")
engine = Engine("engine.py")
# this silly loop is a sample of how the program can be running in
# one thread and the monitoring is performed in another.
while True:
engine.f1()
engine.f2()
time.sleep(1)
if __name__ == "__main__":
main()
engine.py
文件:
# this is "engine.py"
def f1():
print("call to f1")
def f2():
print("call to f2")
日志示例:
INFO:root:received notification of fingerprint change (be1c56097992e2a414e94c98cd6a88d162c96956)
INFO:root:received notification of fingerprint change (dcb434869aa94897529d365803bf2b48be665897)
INFO:root:received notification of fingerprint change (36a0a4b20ee9ca6901842a30aab5eb52796649bd)
INFO:root:received notification of fingerprint change (2e96b05bbb8dbe8716c4dd37b74e9f58c6a925f2)
INFO:root:received notification of fingerprint change (baac96c2d37f169536c8c20fe5935c197425ed40)
INFO:root:received notification of fingerprint change (be1c56097992e2a414e94c98cd6a88d162c96956)
INFO:root:received notification of fingerprint change (dcb434869aa94897529d365803bf2b48be665897)
再次强调,这是一场学术讨论,因为我目前没有热插拔Python代码的需求。然而,我喜欢能够稍微理解运行时,并意识到什么是可能的,什么是不可能的。请注意,加载机制可以添加锁,以防它使用资源,还可以添加异常处理,以防模块加载失败。
有任何评论吗?