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

  • SEARCH
  • Home
  • 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 7175977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:24:51+00:00 2026-05-28T16:24:51+00:00

I am writing some code in Python something like this: import sys try: for

  • 0

I am writing some code in Python something like this:

import sys
try:
    for x in large_list:
        function_that_catches_KeyboardInterrupt(x)
except KeyboardInterrupt:
    print "Canceled!"
    sys.exit(1)

When I try to interrupt the loop, I basically need to hold down Control+C long enough to cancel every invocation of the function for all the elements of large-list, and only then does my program exit.

Is there any way I can prevent the function from catching KeyboardInterrupt so that I can catch it myself? The only way I can think of would be to abuse threading by creating a separate thread just for calling the function, but that seems excessive.

Edit: I checked the offending code (which I can’t easily change), and it actually uses a bare except:, so even sys.exit(1) is caught as a SystemExit exception. How can I escape from the bare except: block and quit my program?

  • 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-05-28T16:24:52+00:00Added an answer on May 28, 2026 at 4:24 pm

    You can rebind the SIGINT handler using the signal library.

    import signal, sys
    def handler(signal, frame):
        print "Canceled!"
        sys.exit(1)
    signal.signal(signal.SIGINT, handler)
    for x in large_list:
        function_that_catches_KeyboardInterrupt(x)
    

    There are a few ways that one can exit when SystemExit is being caught. os._exit(1) will do a c-style exit with no cleanup. os.kill(os.getpid(), signal.SIGTERM) will allow the interpreter some level of cleanup, I believe flushing/closing file handles, etc.

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

Sidebar

Related Questions

So I was writing some code today that basically looks like this: string returnString
I'm writing some code in python and I'm having trouble when trying to retrieve
I'm writing some code (just for fun so far) in Python that will store
I am new to Python, and I'm working on writing some database code using
I'm writing code in python for some sort of daemon that has to execute
I'm writing some code that id like to be able to work with any
I have this python code for opening a .cfg file, writing to it and
What benefit or implications could we get with Python code like this: class some_class(parent_class):
I'm writing some python code that's supposed to parse a CSV file. Calculate the
I'm writing some Python code to scrape websites, and what I'm going to end

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.