0

I am new to flask and python so please pardon me if the answer is obvious. My flask app has a scheduler and which opens many threads at different time that do specific different work and also run some commands on the terminal and get their result.

Now I am a little hesitant in what if something goes wrong and the scheduler is unable to spawn a thread or any other issue come while running the application on production.I have my try catch in place but what if some day I miss a very obvious try catch.I don't want my application to stop on production instead it should log the exception and keep accepting request or scheduling the other task with out stopping the application main thread.

So I wanted to implement a global exception handler that is able to catch any and all types of exception not just the API exception.

python 3.8 flask 3.0

I have tried adding the below code but the application stops after logging and in case of exception on my thread it is unable to catch the exception.

def global_exception_handler(exc_type, exc_value, exc_traceback):
    try:
        # Log or handle the exception here
        print(f"##########Global exception handler caught: {exc_value}")
    except Exception as e:
        print(f"##########Global exception unable to handle exception")


sys.excepthook = global_exception_handler

and this also

@app.errorhandler(Exception)
def handle_exception(error):
    response = jsonify({'message': str(error)})
    response.status_code = 500
    return response

1 Answer 1

0

Luckily, Python already does a great job handling threads and when they crash. Nothing will happen if a thread crashes, it will log and move on. Of course you should add a try/except if you plan to do something if they crash

Although, one thing I would recommend is setting the threads to daemon (I have no idea why it sounds like demon 🤣). This will ensure that all the threads are closed at the end of the program.

Sorry, its hard to help more without more code as it depends on how fatal it would be if a thread crashed

4
  • I just wanted to make sure in no condition the system crashes unless it is a database error or errors that does not let the system start like in spring boot we can create our own global exception handler and manage everything on our own when a system gets an exception and pass our own custom error logs.
    – xyz
    Commented Jul 11 at 4:23
  • This is in general for any flask app not just mine so I have not provided the application code.If you need any explanation or specific code flow I am up for that.
    – xyz
    Commented Jul 11 at 4:25
  • Well your application should never crash due to threads. The only chance of that is if the thread does something fatal to your process like if you run out of memory or if you modify something that causes the main loop to crash
    – SimplyDev
    Commented Jul 12 at 11:23
  • Although a try/except on the thread itself won’t hurt. But there is no real way to make sure the app doesn't crash on production. There will always be something that can crash, Flask tries its best to log and move on, but its possible to still crash the program. If you want, you can change Flask’s logging settings by importing the logging module and setting the destination file of your error logs
    – SimplyDev
    Commented Jul 12 at 11:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.