Terminateable thread
Diez B. Roggisch
deets at nospam.web.de
Thu Jun 8 08:18:02 EDT 2006
Laszlo Nagy wrote:
>
> 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.lang.python/browse_frm/thread/bf5461507803975e/3008575be66f99c1?lnk=st&q=alex+martelli+python+GIL&rnum=2#3008575be66f99c1
More information about the Python-list
mailing list