[Python-3000] improved threading in py3k

Josiah Carlson jcarlson at uci.edu
Fri Aug 4 20:17:49 CEST 2006


"tomer filiba" <tomerfiliba at gmail.com> wrote:
> python's threading model seems too weak imo. i'm not talking about
> the GIL and the fact threads run one at a time -- i'm talking about the
> incompleteness of the API of thread module.

I could have sworn that it could be implemented as a debugging trace
function [1], but my tests [2] seem to imply that non-mainthread code
doesn't actually have the trace function called.

 - Josiah

[1]

>>> import sys
>>> import threading
>>>
>>> kill_these = {}
>>>
>>> def killthread(thread):
...     kill_these[thread] = None
...
>>> def trace(*args):
...     del args
...     if threading.currentThread() in kill_these:
...         #pick some exception unlikely/impossible to catch
...         raise MemoryError
...     return trace
...
>>> sys.settrace(trace)
>>> def waster():
...     while 1:
...             a = 1
...             b = 2
...             c = 3
...
>>> x = threading.Thread(target=waster)
>>> x.start()
>>> killthread(x)
>>> kill_these
{<Thread(Thread-1, started)>: None}
>>> x in kill_these
True
>>> x in threading.enumerate()
True
>>> threading.enumerate()
[<Thread(Thread-1, started)>, <_MainThread(MainThread, started)>]
>>> 


[2]
>>> import threading
>>> import sys
>>> seen = {}
>>> def trace(*args):
...     x = threading.currentThread()
...     if x not in seen:
...             print x
...     seen[x] = None
...     return trace
...
>>> sys.settrace(trace)
>>> def waster():
<_MainThread(MainThread, started)>
...     while 1:
...             a = 1
...             b = 2
...             c = 3
...
>>> x = threading.Thread(target=waster)
>>> x.start()
>>>

This is in Python 2.4.3 on Windows.

> - - - -
> 
> (*) about os._exit -- how about introducing os.exit, which would serve
> as the "nicer" version of os._exit? os.exit would kill the process in
> the same way SystemExit kills it (performing cleanups and all).
> in fact, the interpreter would just call os.exit() when catching SystemExit.

Already exists as sys.exit()

 - Josiah



More information about the Python-3000 mailing list