This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: importing threading in a thread does not work
Type: behavior Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: ajaksu2, brett.cannon, josiahcarlson, steevel
Priority: normal Keywords:

Created on 2006-09-21 11:59 by steevel, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg61001 - (view) Author: Jaster (steevel) Date: 2006-09-21 11:59
I got across this while trying to use MySQLdb in a
thread. Since MySQLdb imports decimal I got the same
error there.

Code: (This happens in both 2.4 and 2.5)

import thread, time, sys
if len(sys.argv) > 1:
   import threading

def test ():
   import decimal
   print 'Exiting test.'

thread.start_new_thread(test, ())
time.sleep(1)


Output:
$ ./thread.py 1
Exiting test.

$ ./thread.py
Exiting test.
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/local/python2.5/lib/python2.5/atexit.py",
line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File
"/usr/local/python2.5/lib/python2.5/threading.py", line
656, in __exitfunc
    self._Thread__delete()
  File
"/usr/local/python2.5/lib/python2.5/threading.py", line
540, in __delete
    del _active[_get_ident()]
KeyError: -1209698640
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/usr/local/python2.5/lib/python2.5/atexit.py",
line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File
"/usr/local/python2.5/lib/python2.5/threading.py", line
656, in __exitfunc
    self._Thread__delete()
  File
"/usr/local/python2.5/lib/python2.5/threading.py", line
540, in __delete
    del _active[_get_ident()]
KeyError: -1209698640
msg61002 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2006-09-22 03:48
Logged In: YES 
user_id=341410

I believe it is an issue with the import lock.

If you 'import decimal' prior to starting up your subthread,
everything will run fine.
msg61003 - (view) Author: Jaster (steevel) Date: 2006-09-22 17:56
Logged In: YES 
user_id=1117967

I don't get this interface. Why can't I post a comment?
To josiahcarlson: The program I'm currently writing is
module based and I don't know what python modules my users
will import. It shouldn't matter tho, either this is a "bug"
or it needs to go in the documentation that you can't import
them in a thread if you havent already imported them before.
msg61004 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2006-09-22 20:25
Logged In: YES 
user_id=341410

It's not so much that you can't import in a thread, it's
that you can't import in a thread when the importer is
running in another thread.

When you run a module via 'python module.py', Python
compiles that module into bytecode, then imports the module,
which results in the module being executed.  The execution
of the module performs the various imports, function
definitions, class definitions, etc.  When you start up a
thread while the module is being imported, the import lock
doesn't know what to do and bails out, leaving you with the
situation you are having.

Try rewriting your module like:
import thread
import time
import decimal

def test():
    import decimal
    print "exiting test"

def main():
    thread.start_new_thread(test, ())
    time.sleep(1)

Then running it via:
    python -c "import tmodule;tmodule.main()"

And really, aside from "importing in a thread can be
troublesome, if you don't know what you are doing, don't do
it", what kind of documentation change would you suggest,
and where would it go?
msg81698 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-02-12 00:58
Here's the output of trunk (debug) for OP's test:

./python ../thread.py
Exiting test.
Exception KeyError: KeyError(-1210869568,) in <module 'threading' from
'~/trunk-py/Lib/threading.pyc'> ignored
[31071 refs]
msg84713 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2009-03-30 23:21
So the failure stems from threading being imported by decimal in a
thread itself. This is just not going to be supported as it confuses
threading as to what thread was the main thread.

Closing as "won't fix".
History
Date User Action Args
2022-04-11 14:56:20adminsetgithub: 44003
2009-03-30 23:21:11brett.cannonsetstatus: open -> closed
resolution: wont fix
messages: + msg84713

title: decimal module borks thread -> importing threading in a thread does not work
2009-02-12 00:58:31ajaksu2setnosy: + ajaksu2
type: behavior
messages: + msg81698
2009-02-11 03:12:23ajaksu2setassignee: brett.cannon
nosy: + brett.cannon
2006-09-21 11:59:41steevelcreate