Why Does Sys.Exit() Not Exit When Called Inside a Thread in Python

Why does sys.exit() not exit when called inside a thread in Python?

sys.exit() raises the SystemExit exception, as does thread.exit(). So, when sys.exit() raises that exception inside that thread, it has the same effect as calling thread.exit(), which is why only the thread exits.

What does sys.exit really do with multiple threads?

As per the documentation sys.exit() raises SystemExit:

Exit the interpreter by raising SystemExit(status).

If SystemExit reaches the default exception handler,
it calls handle_system_exit(), which more or less pushes through to Py_Finalize(), which in turn calls wait_for_thread_shutdown() in Python 2, so sys.exit() is the same as the normal falling off the bottom of the main module in waiting for all non-daemon threads to terminate.

Why are the methods sys.exit(), exit(), raise SystemExit not working?

The problem is that all sys.exit() does is raise SystemExit. Since this happens in a worker thread, the effect is to stop that thread (exceptions don't propagate across threads).

You could trying signalling to the main thread that the script needs to terminate, either though some mechanism of your own, or by calling thread.interrupt_main().

For a sledgehammer approach, call os._exit().

Terminate python threads using sys.exit()

sys.exit() really only raises the SystemExit exception and it only exits the program if it is called in the main thread. Your solution "works" because your thread doesn't catch the SystemExit exception and so it terminates. I'd suggest you stick with a similar mechanism but use an exception you create yourself so that others aren't confused by a non-standard use of sys.exit() (which doesn't really exit).

class MyDescriptiveError(Exception):
pass

def my_function():
raise MyDescriptiveError()

Python script not stopping on sys.exit()

Sometimes, when writing a multithreadded app, raise SystemExit and sys.exit() both kills only the running thread. On the other hand, os._exit() exits the whole process.

While you should generally prefer sys.exit because it is more "friendly" to other code, all it actually does is raise an exception.

If you are sure that you need to exit a process immediately, and you might be inside of some exception handler which would catch SystemExit, there is another function - os._exit - which terminates immediately at the C level and does not perform any of the normal tear-down of the interpreter

A simple way to terminate a Python script early is to use the built-in quit() function. There is no need to import any library, and it is efficient and simple.

Example:

       #do stuff
if this == that:
quit()

You may try these options!!
Hope it works fine!! If not tell us, we will try more solutions!

Python - Starting a thread kills all threads using sys.exit()

Your code never starts a thread.

In main(), you do this:

Thread(target=func_that_calls_sys_exit(foo, bar), ...)

when you mean to do this:

Thread(target=func_that_calls_sys_exit, args=(foo, bar), ...)

The former invokes the method before any thread is created, intending to pass its return value to the Thread constructor's target parameter. That invocation of course raises a SystemExit, thus terminating your process before any threads can be created.

Why does my exit() command not work in python?

exit() (some forms; it's not actually supposed to be used in scripts, so it can be overridden to do weird things; you want sys.exit()) is implemented by raising a SystemExit exception (exiting without bubbling out would mean cleanup blocks in finally and with statements don't execute, which would be bad, so they use the exception mechanism to do the cleanup). SystemExit is special in two ways:

  1. It doesn't dump a traceback and exit with a fixed status 1 if unhandled (it just silently exits the program with the status code it was provided)
  2. It's not a child of Exception, so except Exception:, which is very broad, still won't catch it

But you used a bare except, which catches everything, including SystemExit. Don't do that.

In this specific case, the correct solution is really to just replace exit() with break, which will pop you out of the while loop and allow the program to run to completion with no need for an exception at all. And use a narrower except clause while you're at it.



Related Topics



Leave a reply



Submit