python theading.Timer: how to pass argument to the callback?
Timer
takes an array of arguments and a dict of keyword arguments, so you need to pass an array:
import threadingdef hello(arg): print argt = threading.Timer(2, hello, ["bb"])t.start()while 1: pass
You're seeing "b" because you're not giving it an array, so it treats "bb"
an an iterable; it's essentially as if you gave it ["b", "b"]
.
kwargs
is for keyword arguments, eg:
t = threading.Timer(2, hello, ["bb"], {arg: 1})
See http://docs.python.org/release/1.5.1p1/tut/keywordArgs.html for information about keyword arguments.
The third argument to Timer
is a sequence. Since you pass "bb" as that sequence, hello
gets the elements of that sequence ("b" and "b") as separate arguments (arg
and kargs
). Put "bb" in a list and hello
will get the string as the first argument.
t = threading.Timer(2, hello, ["bb"])
As for hello
's parameters, you probably mean:
def hello(*args, **kwargs):
The meaning of **kwargs
is covered in the queston "What does *args and **kwargs mean?"