exception capture in threads and parent

2024/10/15 7:21:47

How do you nicely capture exceptions in Python threads?

If you have a threaded python app sys.excepthook does not capture errors in children.

When a child raises an exception the parent will continue to run without any problems thus making debugging a bit difficult.

Answer
import os
import sys
import signal
import string
import threading# capture Exceptions
def except_catch(type, value, track, thread):import tracebackrawreport = traceback.format_exception(type, value, track)report = "\n" . join(rawreport)errorlog = open("errors.log", "a")if thread != "":errorlog.write("Exception in thread: " + thread + "\n\n")errorlog.write(("%s\n" + "-" * 30 + "\n\n") % report)errorlog.close()
sys.excepthook = except_catch# capture KeyboardInterrupt
def interrupt_catch(signal, frame):print ""os._exit(1)
signal.signal(signal.SIGINT, interrupt_catch)# all your threaded code here
def whatever_threaded_function():try:a = 1 / 0except:exc_type, exc_value, exc_traceback = sys.exc_info()except_catch(exc_type.__name__, exc_value, exc_traceback, threading.current_thread().name)threading.Thread(target=whatever_threaded_function).start()
https://en.xdnf.cn/q/117861.html

Related Q&A

Writing a program that compares 2 numbers in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Run Python3 without activating the virtual environment

My objective is to run Python 3 code on the AWS Lambda Service, which currently only supports Python 2.7. These are the steps I have done.Since I work on a Mac, setup a docker image similar to the AWS …

Matplotlib functions in tkinter

This is my first python project so I understand that this problem may seem a bit stupid. I am trying to create a Mandelbrot renderer. I am piecing code together from tutorials and code that I understan…

sudo su user -c with arguments not working

I am trying to execute command from python as another "user" with:command = "sudo su user -c player --standard=1 -o 2" subprocess.Popen(command.split(), shell=False, stdin=None, std…

Grouping data on column value

Hi I have data (in excel and text file as well) like C1 C2 C31 p a1 q b2 r c2 s dAnd I want the output like:C1 C2 C31 p,q a,b2 r,s c,dHow can I group the data…

Memory Error Python Processing Large File Line by Line

I am trying to concatenate model output files, the model run was broken up in 5 and each output corresponds to one of those partial run, due to the way the software outputs to file it start relabelling…

python assign literal value of a dictionary to key of another dictionary

I am trying to form a web payload for a particular request body but unable to get it right. What I need is to pass my body data as below data={file-data:{"key1": "3","key2&quo…

python regex findall span

I wanna find all thing between <span class=""> and </span> p = re.compile(<span class=\"\">(.*?)\</span>, re.IGNORECASE) text = re.findall(p, z)for exampl…

Why cant I view updates to a label while making an HTTP request in Python

I have this code :def on_btn_login_clicked(self, widget):email = self.log_email.get_text()passw = self.log_pass.get_text()self.lbl_status.set_text("Connecting ...")params = urllib.urlencode({…

plotting multiple graph from a csv file and output to a single pdf/svg

I have some csv data in the following format.Ln Dr Tag Lab 0:01 0:02 0:03 0:04 0:05 0:06 0:07 0:08 0:09 L0 St vT 4R 0 0 0 0 0 0…