4

I want to make a program which is checks the given url for every 10 seconds

def listener(url):
    print ("Status: Listening")
    response = urllib.request.urlopen(url)
    data = response.read()
    text = data.decode('utf-8')
    if text == 'Hello World. Testing!':
        print("--Action Started!--")


t = Timer(10.0,listener('http://test.com/test.txt'))
t.start()

here is the output:

Status: Listening
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.4/threading.py", line 1186, in run
    self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable

It runs the function for one time, after 10 seconds the error appears

1 Answer 1

12

Currently, the result of your listener function call is being given to Timer, which is resultantly None, as you need to make an explicit return so your Timer has something to operate on.

You have to place the argument for your listener() in another parameter, like this. Note that args must be a sequence, thus the tuple with the comma placed after your url. Without this tuple, you are passing each individual character from 'http://test.com/test.txt' as an argument to listener.

t = Timer(10.0,listener,args=('http://test.com/test.txt',))

As you can see in documentation here, arguments must be passed as a third parameter.

Sign up to request clarification or add additional context in comments.

3 Comments

What does the return type of the listener() have to be? I set the return as a string and got: Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 1082, in run self.function(*self.args, **self.kwargs) TypeError: 'str' object is not callable
Also, for some reason t = Timer(30.0, self.killDBConnection()) is not working as expected for me. I want to run the self.killDBConnection() only after 30 seconds, but it get run immediately instead of after 30 seconds
This saved me, had a timer inside a supprocess.Popen() call and if it does not work it will not signal anything, moved it out and I saw the error from threading.Timer: noneType not callable. After I got it working on the outside, I could put it back again.

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.