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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:42:45+00:00 2026-06-09T05:42:45+00:00

I am testing Python threading with the following script: import threading class FirstThread (threading.Thread):

  • 0

I am testing Python threading with the following script:

import threading

class FirstThread (threading.Thread):
    def run (self):
        while True:
            print 'first'

class SecondThread (threading.Thread):
    def run (self):
        while True:
            print 'second'

FirstThread().start()
SecondThread().start()

This is running in Python 2.7 on Kubuntu 11.10. Ctrl+C will not kill it. I also tried adding a handler for system signals, but that did not help:

import signal 
import sys
def signal_handler(signal, frame):
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

To kill the process I am killing it by PID after sending the program to the background with Ctrl+Z, which isn’t being ignored. Why is Ctrl+C being ignored so persistently? How can I resolve this?

  • 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-09T05:42:47+00:00Added an answer on June 9, 2026 at 5:42 am

    Ctrl+C terminates the main thread, but because your threads aren’t in daemon mode, they keep running, and that keeps the process alive. We can make them daemons:

    f = FirstThread()
    f.daemon = True
    f.start()
    s = SecondThread()
    s.daemon = True
    s.start()
    

    But then there’s another problem – once the main thread has started your threads, there’s nothing else for it to do. So it exits, and the threads are destroyed instantly. So let’s keep the main thread alive:

    import time
    while True:
        time.sleep(1)
    

    Now it will keep print ‘first’ and ‘second’ until you hit Ctrl+C.

    Edit: as commenters have pointed out, the daemon threads may not get a chance to clean up things like temporary files. If you need that, then catch the KeyboardInterrupt on the main thread and have it co-ordinate cleanup and shutdown. But in many cases, letting daemon threads die suddenly is probably good enough.

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

Sidebar

Related Questions

While writing and testing a python method, I am currently using the following approach:
I'm testing on my python script: #!/usr/bin/python import os,sys import glob import commands import
I'm running into an issue while unit-testing a Python project that I'm working on
I'm looking for a tool to record scripts for web application testing in python,
I'm testing distributing a Python extension module as an egg created with setuptools. Here
I've been doing testing work in Python using QtWebkit/Spynner. As QtWebKit has Javascript support
I'm writing a small web server for testing purposes using python, BasicHTTPServer and SimpleHTTPServer.
I have problem concerning python packages and testing. I'm writing an application using wx
i'm just testing out the csv component in python, and i am having some
We have a python project that we want to start testing using buildbot. Its

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.