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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:27:47+00:00 2026-05-24T14:27:47+00:00

Is there a way to have the parent that spawned a new thread catch

  • 0

Is there a way to have the parent that spawned a new thread catch the spawned threads exception? Below is a real basic example of what I am trying to accomplish. It should stop counting when Exception is raised, but I don’t know how to catch it. Are exceptions thread safe? I would love to be able to use the Subprocess module, but I am stuck using Python 2.3 and am not sure how else to do this. Possibly using the threading module?

import time
import thread

def test(): 
    try:
        test = thread.start_new_thread(watchdog, (5,))
        count(10)
    except:
        print('Stopped Counting')

def count(num):
    for i in range(num):
        print i
        time.sleep(1)

def watchdog(timeout):
    time.sleep(timeout)
    raise Exception('Ran out of time')

if __name__ == '__main__':
    test()

UPDATE

My original code was a little misleading. It am really looking for something more like this:

import time
import thread
import os

def test(): 
    try:
        test = thread.start_new_thread(watchdog, (5,))
        os.system('count_to_10.exe')
    except:
        print('Stopped Counting')

def watchdog(timeout):
    time.sleep(timeout)
    raise Exception('Ran out of time')

if __name__ == '__main__':
    test()

I am trying to create a watchdog to kill the os.system call if the program hangs up for some reason.

  • 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-24T14:27:47+00:00Added an answer on May 24, 2026 at 2:27 pm

    Why not something like this

    def test(): 
        def exeption_cb():
            os._exit()
        test = thread.start_new_thread(watchdog, (5, exception_cb))
        os.system('count_to_10.exe')
        print('Stopped Counting')
    
    def watchdog(timeout, callback):
        time.sleep(timeout)
        callback()
    

    This will stop the entire process. Another thing you could do is to start os.system in a different thread, then countdown and then kill that thread. Something like this,

    def system_call():
        os.system('count_to_10.exe')
    
    system_thread = thread.start_new_thread(system_call)
    time.sleep(timeout)
    system_thread.kill()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C#, is there any built-in way to correct URLs that have parent pathing
Just wondering if there is a way in Spring to have a parent controller:
Is there any way to have something that looks just like a file on
Is there a way to have TortoiseSVN (or any other tool) auto-add any new
Is there a way to have a mutable static variable in F# class that
Is there a way to have a file that is modified / touched whenever
Is there a way to have a RewriteRule fire on any domain that is
Is there a way to have a child DIV within a parent container DIV
Is there some way to have a generic type with a parameterless constructor that
Say I have a parent div with three child divs. Is there a way

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.