Terminateable thread

L

Laszlo Nagy

I wote a TThread class that is 'soft terminateable'. For example:

class MyProcessor(TThread):
def run(self):
while not self.terminated():
self.process_one_item()


My question is that, do I really need to use events for this? Because of
the GIL, assignments are atomic. Is it okay to use a simple
'_terminated' attribute instead of the Event object (see below)? It
would be much better to use a simple 'mythread.terminated' attribute
instead of 'mythread.terminated()' and 'mythread.terminated=True'
instead of 'mythread.terminate()'.

Please advise.

Laszlo


--------------------------

from thread import *
from threading import *
class TermMixIn(object):
"""Terminateable thread mixin class.

Methods in this class are thread safe and
can be used to implement softly terminateable
threads. This class can also be used to create
a main synchronization object."""
def __init__(self):
"""Create a TermMixIn instance.

Default state is not terminated."""
self._terminated = Event()
def terminated(self):
""" @return: if the thread is terminated."""
return self._terminated.isSet()
def terminate(self):
"""Set the terminate flag. Can be called from any thread."""
self._terminated.set()
def checkterminated(self):
"""Check if the thread was terminated.

This sould be periodically called from the softly terminateable
thread itself.
Will raise a L{TerminatedException} if the thread is terminated."""
if self.terminated():
raise TerminatedException()

class TThread(Thread,TermMixIn):
"""Terminateable thread. See the L{TermMixIn} class for details."""
def __init__(self,group=None, target=None, name=None, args=(),
kwargs={}):
TermMixIn.__init__(self)
Thread.__init__(self,group,target,name,args,kwargs)
 
D

Diez B. Roggisch

Laszlo said:
I wote a TThread class that is 'soft terminateable'. For example:

class MyProcessor(TThread):
def run(self):
while not self.terminated():
self.process_one_item()


My question is that, do I really need to use events for this? Because of
the GIL, assignments are atomic. Is it okay to use a simple
'_terminated' attribute instead of the Event object (see below)? It
would be much better to use a simple 'mythread.terminated' attribute
instead of 'mythread.terminated()' and 'mythread.terminated=True'
instead of 'mythread.terminate()'.

I do that all the time - just a simple boolean set on the instance. So far,
it served me well.

However, a recent discussion involving Alex Martelli [1] saying that

"""
You have misread the Python Language Reference -- if you can give the
URL on which you have read any such promise of atomicity, I will be glad
to fix the docs to make that unambiguous.

There is no such promise (there may be implementation accidents in some
specific implementation which happen to make some operation atomic, but
NO guarantee even there that the next bugfix won't break that).
"""

your implementation might be the better solution.

Diez

[1] :
http://groups.google.com/group/comp...x+martelli+python+GIL&rnum=2#3008575be66f99c1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top