Question

I'm trying to extend a TimerTask in Jython to read data from some sensors, and then send that data through the network every one second. In order to do that I need to pass the reader and server objects (as well as some event objects) to the TimerTask object. When I try to do it through the Timer.schedule method I get this error:

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

I gather that the Timer.schedule method calls the run method of the TimerTaskObject, but why doesn't it first call __init__? I've also tried adding the parameters to my TimerTask's run method, but I get the same thing.

Here's my code:

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

I see that in straight Java you simply extend TimerTask and call new MyTimerTask(...) in the Timer.schedule method. I can't seem to make it work that way in Jython. What am I doing wrong?

Thanks for taking a look at this!

Was it helpful?

Solution

Your init should be called __init__:

class IButtonTimerTask(TimerTask):

    def __init__(self, reader, server, enterPressed, runComplete):
        ^^    ^^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top