473,473 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Threading

I am trying to write a thread that will execute a function every 2
seconds until the program is close, in which case it will stop. I can
write the program easy enough to execute the command every 2 seconds,
the problem comes when I try to close the program. It won't close the
thread. Any ideas as to what I can do? Thanks!

Nov 2 '05 #1
4 1622
How do you use threads? With threading.Thread objects? Then this recipe
might help, it did it for me. For further assistance please post your
code.

Nov 2 '05 #2
Tuvas wrote:
I am trying to write a thread that will execute a function every 2
seconds until the program is close, in which case it will stop. I can
write the program easy enough to execute the command every 2 seconds,
the problem comes when I try to close the program. It won't close the
thread. Any ideas as to what I can do? Thanks!


The simplest thing to do is call setDaemon on the thread object:

t = threading.Thread(target=MyFunc, args=(1,2,3))
t.setDaemon(1)
t.start()

This doesn't really give your other thread a chance to shutdown cleanly though,
so if you need to "guarantee" clean shutdown, then have your thread check some
shared flag or have some other way to know when it needs to quit, and then have
the main thread wait for it to terminate by calling Thread.join.

For example, if you have a work queue, you could decide on the convention that
None means all the work is done:

import threading, Queue, time

def Work(q):
while 1:
work = q.get()
if work is None:
break
print 'Working on', work
time.sleep(1)
print 'Worker is quitting now'

q = Queue.Queue() # this queue is shared with the worker thread

# Start up a worker
t = threading.Thread(target=Work, args=(q,))
t.start()

# Give it stuff to do
for i in range(5):
q.put(i)

# signal end of work
q.put(None)

print 'Main thread waiting for worker to be done'
t.join() # returns once all the work is done
print 'All done'

-Dave
Nov 2 '05 #3
Ooops, forgot this link to the recipe:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65448

Nov 2 '05 #4
That helps alot with the Daemon and recipe! Thanks for the help!

Nov 2 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
0
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
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
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,...
1
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...
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,...
1
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...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.