如何在Jython中扩展java TimerTask并将参数传递给它?

时间:2013-01-04 19:45:06

标签: java jython timertask jython-2.5

我正在尝试在Jython中扩展TimerTask以从某些传感器读取数据,然后每隔一秒通过网络发送该数据。为此,我需要将reader和server对象(以及一些事件对象)传递给TimerTask对象。当我尝试通过Timer.schedule方法执行此操作时,我收到此错误:

File "C:\Documents\src\iButtonHandler.py", line 61, in run
TypeError: org.python.proxies.iButtonHandler$IButtonTimerTask$1(): expected 0 args; got 4

我认为Timer.schedule方法会调用TimerTaskObject的run方法,但为什么不首先调用__init__?我也尝试将参数添加到我的TimerTask的{​​{1}}方法中,但我得到同样的结果。

这是我的代码:

run

我在直接Java中看到你只是扩展class IButtonTimerTask(TimerTask): def init(self, reader, server, enterPressed, runComplete): self.__reader = reader self.__server = server self.__enterPressed = enterPressed self.__runComplete = runComplete def run(self): iButtonData = self.__reader.getAllValues() self.__server.sendData(iButtonData): if self.__enterPressed.isSet(): self.cancel() self.__runComplete.set() class IButtonHandler(threading.Thread): def __init__(self, port, container, enterPressedEvent, exitEvent): threading.Thread.__init__(self, name='iButton Handler Thread') print 'creating ibutton thread' self.__container = container self.__reader = IButtonContainerReader(self.__container) self.__containerId = self.__reader.getID() self.__server = MyServer(port, name=self.__containerId) self.__enterPressed = enterPressedEvent self.__exitEvent = exitEvent self.__runComplete = threading.Event() self.start() def run(self): print 'ibutton thread running' if self.__server.listen(): timer = Timer() timer.schedule(IButtonTimerTask(self.__reader, self.__server, self.__enterPressed, self.__runComplete), 0, 1000) self.__runComplete.wait() print 'iButton handler %s exiting' %self.__containerId timer.cancel() self.shutDown() return 并在TimerTask方法中调用new MyTimerTask(...)。我似乎无法在Jython中以这种方式工作。我究竟做错了什么?

谢谢你看看这个!

1 个答案:

答案 0 :(得分:2)

您的init应该被称为__init__

class IButtonTimerTask(TimerTask):

    def __init__(self, reader, server, enterPressed, runComplete):
        ^^    ^^