0

I am trying to use a Timer inside a method, as a way to wait some time, without blocking the script, as time.sleep would.

Outside of the class, the Timer code runs fine, but inside a class method, it returns the error: TypeError: 'NoneType' object is not callable

import time
from threading import Timer

openDuration = 10

### Valve class
class Valve():
    def open(self, openDuration):
        t = Timer(openDuration, Valve().dummyWait())    
        t.start()
        t.join()
        print("Valve open")
    def dummyWait(self):    # empty. Just used to wait some time
        pass

Valve().open(openDuration) 

While the code does run and prints "Valve open" after 10s, it returns this error:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 1166, in run
    self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable

What is causing this error message? It is my understanding, that I am using two variables, openduration & t, both of which get defined, therefore I don't understand the NoneType Error message here.

1 Answer 1

2

Timer takes 2 arguments - time to wait, and function to execute after wait time is reached. Hence, you don't have to create an object when passing the second argument, just pass the function to be run. Try like below snippet:

import time
from threading import Timer

openDuration = 10

### Valve class
class Valve():
    def open(self, openDuration):
        t = Timer(openDuration, self.dummyWait)    
        t.start()
        t.join()
        print("Valve open")
    def dummyWait(self):    # empty. Just used to wait some time
        pass

Valve().open(openDuration)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.