473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 3093
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Developwebsites | last post by:
user enters non-repeating 100 numbers. how do i determine that numbs do not repeat? is this code correct? also, once a repeated number has been found, lets say I enter 345 twice, how do i break...
5
by: news | last post by:
Well, I wrote my first PHP class today. Yeah! But to get it to work, in each function within the class I have to repeat the database connection lines, and that just seems redundant; there has to...
0
by: dino07 | last post by:
Hi All, I am currently trying to do the following: 1. insert a value into a repeating table from a drop-down list( secondary storage) when the user click the "Add" button positioned next to the...
11
by: Christoph Boget | last post by:
When building a form using Infopath, you can define a repeating section and stick form fields in that section. I'm curious if ASP.NET has a similar control to make it easy to design something...
2
by: nickheppleston | last post by:
I'm trying to iterate through repeating elements to extract data using libxml2 but I'm having zero luck - any help would be appreciated. My XML source is similar to the following - I'm trying to...
44
by: shuisheng | last post by:
Dear All, Assume there are three classes where CA has members of class CA1 and CA2 as follows. To make the public functions of CA1 and CA2 can work on the members a1 and a2 in a CA object, I...
0
by: BA | last post by:
I posted on this once before and could not get a solution, I am hoping someone can help. I have a very strange code debug behavior that I cannot make heads or tails of: I have c# code being...
0
by: Killer42 | last post by:
This is a simple VB6 function to generate random numbers in the specified range, without repeating any numbers. New, and only briefly tested. Use at your own risk. :) Option Explicit Private...
3
by: bagelman | last post by:
Hi, I want to find repeating words in a long string with Regular Expressions. I tried to write a regular expression but it didn't work. "\b(?<word>\w+)\s+(\k<word>)\b" This RegEx finds...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.