Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Twisted代码重新加载的局限性_Python_Twisted - Fatal编程技术网

Python Twisted代码重新加载的局限性

Python Twisted代码重新加载的局限性,python,twisted,Python,Twisted,我正在使用Twisted构建一个XML-RPC服务器,该服务器定期检查其源文件的时间戳是否已更改,并使用rebuild重新加载它们 from twisted.python.rebuild import rebuild rebuild(mymodule) 服务器公开的函数可以很好地重新加载,但另一个协议类处于活动状态,该类在同一类的mymodule上调用回调函数,但它们不使用重新加载的函数版本。该协议只是有一个dict,其值为正常函数 我发现了这个mixin类,它旨在处理重建的限制 from

我正在使用Twisted构建一个XML-RPC服务器,该服务器定期检查其源文件的时间戳是否已更改,并使用
rebuild
重新加载它们

from twisted.python.rebuild import rebuild

rebuild(mymodule)
服务器公开的函数可以很好地重新加载,但另一个协议类处于活动状态,该类在同一类的
mymodule
上调用回调函数,但它们不使用重新加载的函数版本。该协议只是有一个
dict
,其值为正常函数

我发现了这个mixin类,它旨在处理
重建的限制

from twisted.python.rebuild import rebuild

rebuild(mymodule)


如何确保我的回调使用最新代码?

您是正确的;这是twisted.python.rebuild的一个限制。更新现有实例的
\uuuuu class\uuuu
属性,以及导入的函数和类

解决这个问题的一种方法是简单地为这种情况修复
重建

但是,如果您想使用
敏感
达到预期目的,还可以实现回调保持类,以便在Twisted的当前版本上使用
重建
。以下示例演示如何使用相关类的所有三个方法更新指向函数的回调字典。请注意,即使没有
if needRebuildUpdate…
测试,直接调用
x()
也将始终获得最新版本

from twisted.python.rebuild import rebuild, Sensitive
from twisted.python.filepath import FilePath

p = FilePath("mymodule.py")
def clearcache():
    bytecode = p.sibling("mymodule.pyc")
    if bytecode.exists():
        bytecode.remove()
clearcache()
p.setContent("def x(): return 1")
import mymodule
from mymodule import x
p.setContent("def x(): return 2")

class Something(Sensitive, object):
    def __init__(self):
        self.stuff = {"something": x}
    def invoke(self):
        if self.needRebuildUpdate():
            for key in list(self.stuff):
                self.stuff[key] = self.latestVersionOf(self.stuff[key])
            self.rebuildUpToDate()
        return self.stuff["something"]()

def test():
    print s.invoke()
    print x()

s = Something()
test()

clearcache()
rebuild(mymodule)

test()