Mark P Neyer
Mark P Neyer

Reputation: 1009

how can i make gevent application exit immediately after any exception?

i'm working with an application written in python using gevent. i want it to exit immediately as a result of any exception that i haven't explicitly trapped.

it looks like i'd have to patch the core gevent code.

is there any way can i do this in my app, without patching gevent or greenlet?

Upvotes: 2

Views: 1811

Answers (2)

suzanshakya
suzanshakya

Reputation: 3650

You can use spawn_link_exception instead of spawn.

Upvotes: 0

Denis
Denis

Reputation: 3780

You you're using version 1.0beta then tweaking gevent.get_hub().SYSTEM_ERROR can help you. SYSTEM_ERROR lists exception types that, if caught by gevent, should be re-raised in the main greenlet.

By default, SYSTEM_ERROR is (KeyboardInterrupt, SystemExit, SystemError). Every other unhandled exception will be just reported but will not end the process (unless the original exception is raised in the main greenlet, in which case the usual happens).

You can modify SYSTEM_ERROR:

import gevent
gevent.spawn(int, "xxx").join()  # will merely report ValueError on stderr
gevent.get_hub().SYSTEM_ERROR += (ValueError, )
gevent.spawn(int, "xxx").join()  # will report ValueError, but then it will also re-raise it

See the source where SYSTEM_ERROR is defined.

If you set SYSTEM_ERROR to BaseException, then any unhandled exception will be considered fatal.

Upvotes: 4

Related Questions