Repeating Thread Error
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Fri Jul 6 19:21:00 EDT 2007
On Fri, 06 Jul 2007 14:40:16 -0700, Jonathan Shan wrote:
> I am trying to call a function every 5 seconds. My understanding of
> time.sleep() is during the sleep time everything "stops".
Not "everything", just the thread in which `sleep()` is called.
> However, in my application, there are background processes that must be
> running continuously during the five second interval.
Then start them as threads and they will run all the time.
> Thus, threading.Timer seems like a good function. Here is the relevant
> code:
>
> # background processes
> t = threading.Timer(5.0, function_name, [arguments])
> while True:
> # Do background processes run inside while loop?
> t.start()
>
> The above code generates an error:
> AssertionError: thread already started
>
> Any hints on how to fix the error?
A thread can only be started once but you try to start the `Timer` over
and over. And you don't wait, so even if this would "work" you would
start many, many `Timer`\s as fast as the ``while`` loop runs. The
`Timer`\s would all run the function after five seconds. It's like::
while True:
function_name(*arguments)
just with a five second delay.
> Also, do background processes run inside while loops?
This question doesn't make much sense to me. Do ``while`` loops block
other threads? No of course not.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list