Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python timer 'NoneType' object is not callable error

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

like image 568
JayGatsby Avatar asked Oct 23 '25 14:10

JayGatsby


1 Answers

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.

like image 199
miradulo Avatar answered Oct 26 '25 04:10

miradulo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!