Python Forum
Timer keeps coming up with a non-callable int object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timer keeps coming up with a non-callable int object
#1
Hey guys, birdwatcher here.

I am writing some code for a robot that I am building (the Robocon 2020 robot) and I am trying to set a timer so that after three minutes the robot switches off. However, whenever I run the code this error message pops up multiple times:

Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 1166, in run
self.function(*self.args, **self.kwargs)
TypeError: 'int' object is not callable


Here is my code:

from threading import Timer

while True:
    t = Timer(3, 60)
    t.start()
    t.join()
    if t >= 180:
        print("Time")
Thanks,
birdwatcher.

P.S.
The 'print("time")' code will be substituted for 'sys.exit()'.
Reply
#2
n [36]: threading.Timer?                                                                                                                                                                    
Init signature: threading.Timer(interval, function, args=None, kwargs=None)
Docstring:     
Call a function after a specified number of seconds:

t = Timer(30.0, f, args=None, kwargs=None)
t.start()
t.cancel()     # stop the timer's action if it's still waiting
Init docstring:
This constructor should always be called with keyword arguments. Arguments are:

*group* should be None; reserved for future extension when a ThreadGroup
class is implemented.

*target* is the callable object to be invoked by the run()
method. Defaults to None, meaning nothing is called.

*name* is the thread name. By default, a unique name is constructed of
the form "Thread-N" where N is a small decimal number.

*args* is the argument tuple for the target invocation. Defaults to ().

*kwargs* is a dictionary of keyword arguments for the target
invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke
the base class constructor (Thread.__init__()) before doing anything
else to the thread.
File:           ~/.pyenv/versions/3.8.1/lib/python3.8/threading.py
Type:           type
Subclasses:
The first argument is the time in seconds.
The second argument is the function, which should called after the time is up.
Then you can supply the function with arguments and keyword-arguments.

Example:
from threading import Timer


def my_timer_callback(name, greeting="Hello {}"):
    print(greeting.format(name))

# my_timer_callback('Andre')
# my_timer_callback('Andre', greetings="Hallo {}")

my_kwargs = {"greeting": "Hallo {}"}
my_timer = Timer(5, my_timer_callback, args=["Andre"], kwargs=my_kwargs)
# nothing happens until you start the timer
my_timer.start()

# or the shortcut
Timer(5, my_timer_callback, args=["Andre"], kwargs=my_kwargs).start()
You can use any callable. Here a minimal example:
Timer(1, print, args=["Hello World"]).start()
As you can see, I use the square brackets instead of a tuple.
The problem is, that parenthesis are also used for grouping of expressions.

Timer(1, print, args=("Hello World")).start()
The result is:
Output:
H e l l o W o r l d
The cause is, that the parenthesis around "Hello World" is not a tuple.
Then *args is consuming the string. Each character is now an argument for the print_function.
The print_function puts a whitespace between the arguments.

Right is:
Timer(1, print, args=("Hello World",)).start()
Do you see the comma after "Hello World"? This is only important, if you want to make a tuple with only one element inside.

I use for this the list literal []:
Timer(1, print, args=["Hello World"]).start()
There is no comma needed, if you only have one element.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 456 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  TypeError: 'NoneType' object is not callable akbarza 4 921 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,262 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 778 Jul-23-2023, 02:25 PM
Last Post: Fare
  TypeError: 'float' object is not callable #1 isdito2001 1 1,046 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  'SSHClient' object is not callable 3lnyn0 1 1,132 Dec-15-2022, 03:40 AM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,375 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,834 Oct-30-2022, 12:53 PM
Last Post: makeeley
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,495 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  [split] TypeError: 'int' object is not callable flash77 4 2,692 Mar-21-2022, 09:44 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020