471,342 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

Repeating Thread Error

Hello,

I am trying to call a function every 5 seconds. My understanding of
time.sleep() is during the sleep time everything "stops". However, in
my application, there are background processes that must be running
continuously during the five second interval. 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? Also, do background processes run
inside while loops?

Thanks in advance,
Jonathan Shan

Jul 6 '07 #1
3 3013
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
Jul 6 '07 #2
hmm.. why use while True? After 5 secs, the function is going to run. so

t = threading.Thread(5.0, func)
t.start()

should just work. Put it in a infinite loop will start the thread and
then start a the stopped thread... forever.

Jim
On Jul 6, 2007, at 2:40 PM, Jonathan Shan wrote:
Hello,

I am trying to call a function every 5 seconds. My understanding of
time.sleep() is during the sleep time everything "stops". However, in
my application, there are background processes that must be running
continuously during the five second interval. 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? Also, do background processes run
inside while loops?

Thanks in advance,
Jonathan Shan

--
http://mail.python.org/mailman/listinfo/python-list
Jul 6 '07 #3
Jonathan Shan wrote:
Hello,

I am trying to call a function every 5 seconds. My understanding of
time.sleep() is during the sleep time everything "stops". However, in
my application, there are background processes that must be running
continuously during the five second interval. 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? Also, do background processes run
inside while loops?

Thanks in advance,
Jonathan Shan
Here's a little thread-test program I wrote a few years ago. I hope it
will explain how threads and sleeps interact.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
import time, threading, random

class MyThread(threading.Thread):
""" Each thread picks a 'random' integer between 0 and 19 and reports
in once per second for that many seconds.
"""

def run(self):
iterations = random.randint(0, 19)
print "Thread", self.getName(), "starting", iterations, "iterations"
for i in range(iterations):
print " ", self.getName(), "is reporting "
time.sleep(1)
print self.getName(), "is DONE"

def test():
threadList = []
iterCount = 0

# Create 5 MyThread() threads
for i in range(5) :
thread = MyThread()
threadList.append(thread)

# Start all threads
for thread in threadList:
thread.start()

# As long as we have more than just the 'main' thread running, print out
# a status message
while threading.activeCount() 1 :
print "-- after", iterCount, "sleeps", \
str(threading.activeCount()), "threads running including main"
iterCount += 1
time.sleep(1)
print "Only main thread remains"

if __name__ == '__main__':
test()

Jul 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Developwebsites | last post: by
reply views Thread by dino07 | last post: by
11 posts views Thread by Christoph Boget | last post: by
2 posts views Thread by nickheppleston | last post: by
44 posts views Thread by shuisheng | last post: by
reply views Thread by BA | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.