【发布时间】:2014-06-14 00:49:10
【问题描述】:
我正在使用 win32com 包与 windows 应用程序交互(应用程序并不重要)。
简而言之,我想要实现的是订阅更新的表。
我已经成功实现了一个回调,它在更新表时接收返回的数据,但我现在需要对收到的数据采取行动。
如果我可以使用附加参数实例化回调对象,这个问题将很容易解决(见下面的代码)但我不知道如何做到这一点。
回调类:
class callBackEvents(object):
""" Callback Object for win32com
"""
def OnNewData(self, XMLData):
logging.info("Subscription returned information")
print "HERE : {}".format(XMLData))
# Would like to use some argument to access logic
# For how to use the new data
def OnActionResult(self, job, msg):
return True
def OnServerDisconnect(self):
logging.debug("Server Disconnected")
def OnServerConnect(self):
logging.debug("Trader Connected To Server")
实例化回调对象:
# Instantiate API com object
self.app = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
# I would like to give the callback object extra arguments e.g. callBackEvents(params)
编辑 实例化回调对象:
# Instatiate two com objects
self.com1 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
self.com2 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
# Create multiple subscriptions (Note these are asynchronous)
# Pushing the subscribed info is not a problem and done elsewhere
self.com1.Subscribe(<subscription info>)
self.com2.Subscribe(<subscription info>)
现在,当订阅信息遇到回调对象时,我不知道是哪个 com 对象设置了订阅(我可以根据返回的信息猜测,但是在设置相同的订阅时这会导致问题)
【问题讨论】:
-
如果回调是一个函数,那么callback - Python, how to pass an argument to a function pointer parameter? - Stack Overflow 可以这样做......但在这种情况下,它是一个 class 对象。动态创建类对象是一种选择。 (奇怪的 API)
标签: python callback arguments win32com