1

I am trying to add a atexit handler to my code. But I find that if I have imported threading module, it gives me an KeyError exception. Is this a bug in python threading module?

#!/usr/bin/python2.7

import threading

# Register a signal handler to exit gracefully
def exit_gracefully():
    print 'Exiting ...'
    import sys
    sys.exit(1)

import atexit
atexit.register(exit_gracefully)

On running the above script, I get

Exiting ...
Exception KeyError: KeyError(139697538152192,) in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
5
  • You are not using threading module. Have you tried removing it? Does the error stay? Commented Mar 23, 2015 at 15:04
  • Also, can you try the same code piece in python shell >>> just the import threading. Does it give the same error? Commented Mar 23, 2015 at 15:11
  • Ofcourse I know I am not using threading module. But in need to use threading module, and thats causing trouble. This script is just a stripped down version of the actual script that shows the problem Commented Mar 23, 2015 at 16:17
  • The issue is seen on interactive shell too on exiting it by pressing Ctrl+D Commented Mar 23, 2015 at 16:17
  • 1
    Couldn't find anybody else on the internet with the same problem as me. I was running into the same issue! I think there is a bug with the atexit module. I'm not importing threading in my code and still getting the same key error above. Commented Jun 16, 2015 at 20:06

1 Answer 1

1

In my case, I get this issue only when threading module was loaded. I managed to solve it by not invoking sys.exit within exit handler.

#!/usr/bin/python2.7

import threading

# Register a signal handler to exit gracefully
def exit_gracefully():
    print 'Exiting ...'

import atexit
atexit.register(exit_gracefully)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.