Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:48:51+00:00 2026-06-14T07:48:51+00:00

I had some Python code that needed to be able to handle SIGINT. For

  • 0

I had some Python code that needed to be able to handle SIGINT. For this purpose I used something like this:

def mymethod(*params):
  obj = MyObj(params)
  try:
    obj.do_some_long_stuff()
  except KeyboardInterrupt:
    obj.cleanup()

Awesome and really straightforward. Yay, Python is great!

However, I now need to also handle other signals, namely SIGTSTP and SIGQUIT. What I’m trying to do is something similar. Here’s some pseudocode demonstrating what I’m trying to do with SIGTSTP (I hope it’s clear enough):

def mymethod(*params):
  obj = MyObj(params)
  try:
    obj.do_some_long_stuff()
  catch SIGINT:
    obj.cleanup()
  catch SIGTSTP:
    log_stop(obj) # Log that we stopped obj (add info about stop signal happening in obj-specific log file )
    raise SIGTSTP # Do normal SIGTSTP behavior as if we didn't catch the signal

It seems that the way to go here is to use the signal module. However, my problem is that I can then can’t access the object state anymore as I could with the KeyboardInterruptException:

import os
import signal

def handler(signum, frame):
  print "I can't access obj from here anymore!" # How to access obj from here?
  signal.signal(signum, signal.SIG_DFL)
  os.kill(os.getpid(), signum) # Rethrow signal, this time without catching it

def mymethod(*params):
  obj = MyObj(params)
  signal.signal(signal.SIGTSTP, handler)
  obj.do_some_long_stuff()   

So how can I solve this, i.e. handle the signal while still having some access to the object I’m working with?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:48:52+00:00Added an answer on June 14, 2026 at 7:48 am

    Or use a closure:

    import os
    import signal
    
    def create_handler(obj):
        def _handler(signum, frame):
            print "obj is availiable here!"
            print obj
            signal.signal(signum, signal.SIG_DFL)
            os.kill(os.getpid(), signum) # Rethrow signal, this time without catching it
        return _handler
    
    def mymethod(*params):
      obj = MyObj(params)
      signal.signal(signal.SIGTSTP, create_handler(obj))
      obj.do_some_long_stuff()   
    

    create_handler(obj) returns a handler function with access to obj.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had some code that worked fine removing punctuation/numbers using regular expressions in python,
I had some code that constructed an object: function gridObjConst(id, itemName, itemPrice, itemListPrice, width,
I had some code that ran commands through Runtime.getRuntime.exec(String) , and it worked on
I had some decryption code (using wincrypt.h ) that lived within my FileReader.cpp class.
Many months back, I had to fix up some code that caused some problems.
This is the code that gets produced when I run python configure.py. dan@Q430-Q530:~/pyinstaller-2.0/PyInstaller$ python
I got some code that i did 3 years ago under python 2.5 or
I have some python code which generates a 256^3 numpy array of data that
Im running into this error that I can't work out Im writing some code
I have a large amount of python code that tries to handle numbers with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.