473,544 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3095
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.Threa d(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(thread ing.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(iteration s):
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.appe nd(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.activ eCount() 1 :
print "-- after", iterCount, "sleeps", \
str(threading.a ctiveCount()), "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
2125
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 out of loop? could someone test this with random(1-1000). const int MAX=100; int Numb; bool exists;
5
2997
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 be a better way that I'm just not bright enough to think of. Any suggestions? (It's the first 3 lines of each of the two functions below. When I...
0
2822
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 drop down list <snip> IXMLDOMNamedNodeMap nnm, dataAttrs; string sCode;
11
4134
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 similar using just ASP.NET (and not Infopath)? I'd hate to think that I'll need to write all the javascript/dhtml to mimic that functionality and I...
2
6610
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 extract the line number and product code from the repeating line elements: <order xmlns="some-ns"> <header> <orderno>123456</orderno> </header>
44
2546
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 just write all the functions such as a1_func1(), a1_fun2(), a2_func1(), as_func2() in the CA interface. I think this is a stupid way since if there are...
0
1786
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 executed in BizTalk assemblies which is repeating debug statements. I tried debug.flush() and debug.close() which did not solve the problem. In my...
0
9317
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 UsedNumber As New Collection Private Const MaxAttempts As Long = 150 Private Const Zero As Long = 0 Private Const One As Long = 1
3
6889
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 repeating words only if they are in consecutive orderçç I need a regular expression pattern that finds repeating words even if they are not ...
0
7414
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7598
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7699
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5895
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5288
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3398
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
651
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.