我正在尝试TimerTask
在 Jython 中扩展 a 以从某些传感器读取数据,然后每隔一秒通过网络发送该数据。为此,我需要将阅读器和服务器对象(以及一些事件对象)传递给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
,但为什么不先调用__init__
呢?我也尝试将参数添加到我TimerTask
的run
方法中,但我得到了同样的结果。
这是我的代码:
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
我看到在纯 Java 中,您只需扩展TimerTask
并调用new MyTimerTask(...)
该Timer.schedule
方法。我似乎无法让它在 Jython 中以这种方式工作。我究竟做错了什么?
谢谢你看看这个!