473,604 Members | 2,487 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 3099
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
2134
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
3012
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 have it fully written, there will be about 10 similar functions, each repeating those three lines.)
0
2830
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
4149
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 don't really feel the need to re-invent the wheel. Has MS designed such a control? A 3rd party? ...
2
6614
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
2568
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 more functions in CA1 and CA2, I need to repeat them all. Is there any good way to implement it?
0
1793
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 BizTalk process I call a static method:
0
9331
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
6895
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 consecutive. I'm new with ASP.Net.
0
8419
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8409
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8065
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8280
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3907
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1266
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.