How to pass arguments to the threading.Timer callback?

Timer expects a sequence (normally, a list or tuple) of arguments and a mapping (normally, a dict) of keyword arguments, so pass a list instead:

import threading

def hello(arg):

t = threading.Timer(2, hello, ["bb"])

while 1:

Since "bb" is an iterable, the Timer will iterate over it and use each element as a separate argument; threading.Timer(2, hello, "bb") is equivalent to threading.Timer(2, hello, ["b", "b"]).

Use a dictionary to pass any keyword arguments to the callback, for example:

def hello(arg, kwarg):
    print('arg is', arg, 'and kwarg is', kwarg)

t = threading.Timer(2, hello, ["bb"], {'kwarg': 1})

Leave a Comment