473,394 Members | 1,679 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,394 software developers and data experts.

Stop a thread on deletion

Hello all,

I'm using threading for generating video content. The trouble is how to
kill the thread, because there are multiple (simultaneous) owners of a
thread. Ideally, a flag would be set when the reference count of the
thread becomes zero, causing the run() loop to quit. Example:

import threading
import time
import gc

class myThread(threading.Thread):
def __init__(self):
self.passedOut = threading.Event()
threading.Thread.__init__(self)
def __del__(self):
self.passedOut.set()
def run(self):
i = 0
while not self.passedOut.isSet():
i += 1
print "Hi %d" % i
time.sleep(0.25)
a = myThread()
a.start()
time.sleep(2.5)
a = None
time.sleep(2.5)

Unfortunately, this doesn't work. When I remove the while-loop, __del__
is called, actually. Appearantly there is still some reference to the
thread while it is running.

I tried gc.get_referrers(self), but it seems to need some parsing. I'm
not sure how to implement that and I'm not sure whether it will work
always or not.

Thanks in advance for any suggestion,
Sjoerd Op 't Land
Aug 8 '07 #1
2 1897
On 8/8/07, Sjoerd Op 't Land <sj****@intercue.nlwrote:
Hello all,

I'm using threading for generating video content. The trouble is how to
kill the thread, because there are multiple (simultaneous) owners of a
thread. Ideally, a flag would be set when the reference count of the
thread becomes zero, causing the run() loop to quit. Example:

import threading
import time
import gc

class myThread(threading.Thread):
def __init__(self):
self.passedOut = threading.Event()
threading.Thread.__init__(self)
def __del__(self):
self.passedOut.set()
def run(self):
i = 0
while not self.passedOut.isSet():
i += 1
print "Hi %d" % i
time.sleep(0.25)
a = myThread()
a.start()
time.sleep(2.5)
a = None
time.sleep(2.5)

Unfortunately, this doesn't work. When I remove the while-loop, __del__
is called, actually. Appearantly there is still some reference to the
thread while it is running.

I tried gc.get_referrers(self), but it seems to need some parsing. I'm
not sure how to implement that and I'm not sure whether it will work
always or not.
gc.get_referrers returns a list of object instances that hold a
reference to the object. The important one in this case is, of course,
the thread itself. The thread holds a reference to the run method
which (of course) requires a reference to the object. In other words,
a running thread cannot be refcounted to zero. You are going to need a
better method of handling your resources.

Perhaps instead of holding a reference to the thread, they could hold
a reference to a proxy object:

import threading
import time
import gc
import pprint

class myThread(threading.Thread):
def __init__(self):
self.passedOut = threading.Event()
threading.Thread.__init__(self)
def run(self):
i = 0
while not self.passedOut.isSet():
i += 1
print "Hi %d" % i
time.sleep(1)
print "stopped"
class ThreadProxy(object):
def __init__(self, proxy_for):
self.proxy_for = proxy_for
def __del__(self):
self.proxy_for.passedOut.set()
def start(self):
self.proxy_for.start()
Aug 8 '07 #2
Dear Cris,

Thanks a lot. This works! (What you didn't know, there was already such
a 'proxy' object in the design, so it isn't the hack it looks ;).)

Thanks again,
Sjoerd Op 't Land

Chris Mellon schreef:
On 8/8/07, Sjoerd Op 't Land <sj****@intercue.nlwrote:
>Hello all,

I'm using threading for generating video content. The trouble is how to
kill the thread, because there are multiple (simultaneous) owners of a
thread. Ideally, a flag would be set when the reference count of the
thread becomes zero, causing the run() loop to quit. Example:

import threading
import time
import gc

class myThread(threading.Thread):
def __init__(self):
self.passedOut = threading.Event()
threading.Thread.__init__(self)
def __del__(self):
self.passedOut.set()
def run(self):
i = 0
while not self.passedOut.isSet():
i += 1
print "Hi %d" % i
time.sleep(0.25)
a = myThread()
a.start()
time.sleep(2.5)
a = None
time.sleep(2.5)

Unfortunately, this doesn't work. When I remove the while-loop, __del__
is called, actually. Appearantly there is still some reference to the
thread while it is running.

I tried gc.get_referrers(self), but it seems to need some parsing. I'm
not sure how to implement that and I'm not sure whether it will work
always or not.

gc.get_referrers returns a list of object instances that hold a
reference to the object. The important one in this case is, of course,
the thread itself. The thread holds a reference to the run method
which (of course) requires a reference to the object. In other words,
a running thread cannot be refcounted to zero. You are going to need a
better method of handling your resources.

Perhaps instead of holding a reference to the thread, they could hold
a reference to a proxy object:

import threading
import time
import gc
import pprint

class myThread(threading.Thread):
def __init__(self):
self.passedOut = threading.Event()
threading.Thread.__init__(self)
def run(self):
i = 0
while not self.passedOut.isSet():
i += 1
print "Hi %d" % i
time.sleep(1)
print "stopped"
class ThreadProxy(object):
def __init__(self, proxy_for):
self.proxy_for = proxy_for
def __del__(self):
self.proxy_for.passedOut.set()
def start(self):
self.proxy_for.start()
Aug 8 '07 #3

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

Similar topics

9
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
1
by: Peter Steele | last post by:
Okay, I assume I'm missing something obvious here. I have created a simple service in C# that on starting spawns a thread to do some processing. The service can be stopped with a "net stop" command...
3
by: Niyazi | last post by:
Hi, I created application that I get information from AS400 for reporting. In main.exe has only 1 frm which calls (as a class library) CLS_MAIN.dll. The CLS_MAIN.dll get the tables from AS400...
2
by: Prasad | last post by:
Hi, I am writing a service which takes a long time to stop after the OnStop call is given by the Services Snap-in. The problem is I cannot cut down on the time that it takes to Stop. The Service...
8
by: Tim Bücker | last post by:
Following scenario: The user opens a form, a thread is started to play a sound ... public void playSoundUsingThread() { if (File.Exists(fileLocation)) PlaySound(fileLocation, 0, 0); //...
26
by: Ricardo | last post by:
I made a program that generate random numbers and put it in a listbox when the user click go. The problem is: how can i made a button stop, to stop the method that is running??? s...
3
by: Saizan | last post by:
I embedded an Rpyc threaded server in a preexistent daemon (an irc bot), this is actually very simple; start_threaded_server(port = DEFAULT_PORT) then I had the necessity to stop the thread which...
9
by: Jon Slaughter | last post by:
I'm using Thread and ThreadStart to create a thread for testing purposes and I do not want to use a pool because the thread exists for the life time of the app. Eventually I might move on to using...
0
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I have a windows application that does not stop running whenever the application exits. Could someone fill me in on what I am doing wrong? Here is the relevant code:...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...

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.