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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:48:07+00:00 2026-05-22T21:48:07+00:00

I cannot catch the INT signal in the main thread, please advise me how

  • 0

I cannot catch the INT signal in the main thread, please advise me how to fix the problem. I wish that the sleep method could be interrupted by CTRL+C, but it waits till the timer is ended..

import pygtk
pygtk.require('2.0')

import gtk
import time
import urllib2
import re
import signal
import sys

import __main__

from time import ctime, strftime, localtime
from threading import Thread

myThread = None

class MyThread(Thread):

    def __init__(self, filename):
        Thread.__init__(self)
        self.filename = filename;
        self.terminate = False

    def StopProcess(self):
        self.terminate = True

    def run(self):
        while self.terminate <> True:
            time.sleep(5)
            self.terminate = True

def SignalHandler(signum, frame):
    if (myThread <> None):
        myThread.StopProcess()
    sys.exit()

if __name__ == "__main__":

    signal.signal(signal.SIGINT, SignalHandler)

    myThread = MyThread("aaa")
    myThread.start()
  • 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-22T21:48:08+00:00Added an answer on May 22, 2026 at 9:48 pm

    Believe it or not, this will work.

    from Queue import Queue, Empty
    def signal_safe_sleep(delay):
      q = Queue()
      try: q.get(True, delay)
      except Empty: pass
    

    Alternatively, you can create some file descriptors using os.pipe() and then use select.select() on them within your signal_safe_sleep function. Both approaches will allow Python signal handlers to be called before signal_safe_sleep returns.

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

Sidebar

Related Questions

I believe that the main thread cannot die before the child thread. But is
I can fix the below exception with a try-catch loop but I cannot understand
Cannot dump Stack Overflow XML file into SQL Server 2008. Could you please explain
I have a method that is structured like this: public object Get(int intId, int
I have an error Cannot make a static reference to the non-static method sendEmptyMessage(int)
The problem i am encountering is that i cannot get my texture to render
Problem: I would like to catch any exceptions from any method in a class
When we can catch an exception like: Violation of UNIQUE KEY constraint 'IX_Product'. Cannot
I cannot understand how this is possible. Please help!! I have an app with
I cannot seem to get my try/catch to work correctly. When you implement a

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.