我喜欢Google App Engine开发环境的一个方面是,每次我更改任何python源文件,其他静态文件甚至配置文件时,我都不必重启服务器。它破坏了我,当我使用其他服务器环境(tornadoweb,web.py,node.js)时,我忘记重启服务器。
任何人都可以解释GAE是如何做到的吗?制作其他服务器(至少基于python)实现同样的目标有多困难?
答案 0 :(得分:3)
您可以查看dev_appserver.py
(link)的来源。看似ModuleManager
复制sys.modules
并监控每个模块以跟踪基于时间的更改:
class ModuleManager(object):
"""Manages loaded modules in the runtime.
Responsible for monitoring and reporting about file modification times.
Modules can be loaded from source or precompiled byte-code files. When a
file has source code, the ModuleManager monitors the modification time of
the source file even if the module itself is loaded from byte-code.
"""
答案 1 :(得分:2)
许多像GAE这样的网络服务器使用python reload模块来查看代码更改中的效果,而无需重新启动服务器进程
import something
if is_changed(something)
somthing = reload(something)
来自python docs的引用: 执行重载(模块)时:
重新编译Python模块的代码并重新执行模块级代码,定义一组新的对象,这些对象绑定到模块字典中的名称。第二次不调用扩展模块的init函数。
与Python中的所有其他对象一样,只有在引用计数降为零后才会回收旧对象。
模块命名空间中的名称将更新为指向任何新对象或已更改对象。 对旧对象的其他引用(例如模块外部的名称)不会反弹以引用新对象,如果需要,必须在每个命名空间中进行更新。