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 9112753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:48:38+00:00 2026-06-17T03:48:38+00:00

Please don’t consider it a duplicate before reading, There are a lot of questions

  • 0

Please don’t consider it a duplicate before reading, There are a lot of questions about multithreading and keyboard interrupt, but i didn’t find any considering os.system and it looks like it’s important.

I have a python script which makes some external calls in worker threads.
I want it to exit if I press ctrl+c But it look like the main thread ignores it.

Something like this:

from threading import Thread
import sys
import os

def run(i):
    while True:
        os.system("sleep 10")
        print i

def main():
    threads=[]
    try:
        for i in range(0, 3):
            threads.append(Thread(target=run, args=(i,)))
            threads[i].daemon=True
            threads[i].start()
        for i in range(0, 3):
            while True:
                threads[i].join(10)
                if not threads[i].isAlive():
                    break

    except(KeyboardInterrupt, SystemExit):
        sys.exit("Interrupted by ctrl+c\n")


if __name__ == '__main__': 
    main() 

Surprisingly, it works fine if I change os.system("sleep 10") to time.sleep(10).

  • 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-17T03:48:39+00:00Added an answer on June 17, 2026 at 3:48 am

    I’m not sure what operating system and shell you are using. I describe Mac OS X and Linux with zsh (bash/sh should act similar).

    When you hit Ctrl+C, all programs running in the foreground in your current terminal receive the signal SIGINT. In your case it’s your main python process and all processes spawned by os.system.

    Processes spawned by os.system then terminate their execution. Usually when python script receives SIGINT, it raises KeyboardInterrupt exception, but your main process ignores SIGINT, because of os.system(). Python os.system() calls the Standard C function system(), that makes calling process ignore SIGINT (man Linux / man Mac OS X).

    So neither of your python threads receives SIGINT, it’s only children processes who get it.

    When you remove os.system() call, your python process stops ignoring SIGINT, and you get KeyboardInterrupt.

    You can replace os.system("sleep 10") with subprocess.call(["sleep", "10"]). subprocess.call() doesn’t make your process ignore SIGINT.

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

Sidebar

Related Questions

*Please don't ask me WHY about a lot of this code. I am running
Please don't consider my question as a duplicate. I just din't succeed trying Display
Before we start: Please don't close this as a duplicate of another question. I
There is some precedent for search-engine-ranking-related questions on StackOverflow, so please don't close this
Please don't confuse with the title as it was already asked by someone but
I did a search before asking so please don't tell me to do that.
I have a flash element (I know, but please don't) with a settings.xml file.
Please don't mind that there is no insert fnc and that data are hardcoded.
Please don't mind about the syntax. I thought of making the post as brief
I'm new in programming so please don't kill me for asking stupid questions. I've

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.