【发布时间】:2014-07-09 02:18:13
【问题描述】:
我正在开发一个项目,旨在通过部分排序信息来增加 Python 套接字消息。我正在构建的库是用 Python 编写的,需要插入到现有系统通过套接字函数发送的消息中。
我已经阅读了一些资源,即@Omnifarious 对这个问题python-importing-from-builtin-library-when-module-with-same-name-exist的回答
有一件你可以做的极其丑陋和可怕的事情 涉及挂钩导入机制。这是你应该做的 可能不会,但它可能会起作用。它转动你的日历 模块成为系统日历模块和您的日历的混合体 模块。
我已经实现了导入机制解决方案,但我们决定这不是我们想要采取的方向,因为它过于依赖环境。在我看来,将类合并到混合中而不是依赖于导入机制的解决方案似乎是最好的方法。
为什么混合动力被称为丑陋可怕的解决方案?我想开始在我的项目中实施它,但我对这些警告持谨慎态度。看起来确实有点骇人听闻,但由于它是安装脚本的一部分,运行一次不就OK了吗?
这是一个代码sn-p,其中插入需要在发送套接字消息之前拦截它:
class vector_clock:
def __init__(self):
"""
Initiate the clock with the object
"""
self.clock = [0,0]
def sendMessage(self):
"""
Send Message to the server
"""
self.msg = "This is the test message to that will be interposed on"
self.vector_clock.increment(0) # We are clock position 0
# Some extraneous formatting details removed for brevity….
# connectAndSend needs interpositioning to include the vector clock
self.client.connectAndSend(totalMsg);
self.client.s.close()
【问题讨论】:
-
请强调问题是什么。我猜你收到了反对票,因为不清楚你在问什么。
-
@kapa 非常感谢您的建议!我将进行编辑以使其更清晰。
-
标题听起来像是在拖钓,而不是引用:/
-
现在好多了,原来的问题也解决了:到处都可以评论:)。
-
你看过猴子补丁套接字吗,gevent (gevent.org/intro.html#monkey-patching) 的做法?我不确定这是否是您所说的“合并”的意思,但是 gevent 人员在一个很多人使用的项目中非常成功地使用了这种方法。
标签: python sockets networking distributed-system