Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring gevent exceptions in jobs

I'm building an application using gevent. My app is getting rather big now as there are a lot of jobs being spawned and destroyed. Now I've noticed that when one of these jobs crashes my entire application just keeps running (if the exception came from a non main greenlet) which is fine. But the problem is that I have to look at my console to see the error. So some part of my application can "die" and I'm not instantly aware of that and the app keeps running.

Jittering my app with try catch stuff does not seem to be a clean solution. Maybe a custom spawn function which does some error reporting?

What is the proper way to monitor gevent jobs/greenlets? catch exceptions?

In my case I listen for events of a few different sources and I should deal with each different. There are like 5 jobs extremely important. The webserver greenlet, websocket greenlet, database greenlet, alarms greenlet, and zmq greenlet. If any of those 'dies' my application should completely die. Other jobs which die are not that important. For example, It is possible that websocket greenlet dies due to some exception raised and the rest of the applications keeps running fine like nothing happened. It is completely useless and dangerous now and should just crash hard.

like image 819
Stephan Avatar asked Feb 09 '12 08:02

Stephan


People also ask

How does gevent work?

Gevent is a library based on non-blocking IO (libevent/libev) and lightweight greenlets (essentially Python coroutines). Non-blocking IO means requests waiting for network IO won't block other requests; greenlets mean we can continue to write code in synchronous style natural to Python.

Is gevent asynchronous?

Gevent is the use of simple, sequential programming in python to achieve scalability provided by asynchronous IO and lightweight multi-threading (as opposed to the callback-style of programming using Twisted's Deferred).

How do I stop gevent?

you can use gevent. killall() !

What does gevent spawn do?

New greenlets are spawned by creating a Greenlet instance and calling its start method. (The gevent. spawn() function is a shortcut that does exactly that). The start method schedules a switch to the greenlet that will happen as soon as the current greenlet gives up control.


1 Answers

I think the cleanest way would be to catch the exception you consider fatal and do sys.exit() (you'll need gevent 1.0 since before that SystemExit did not exit the process).

Another way is to use link_exception, which would be called if the greenlet died with an exception.

spawn(important_greenlet).link_exception(lambda *args: sys.exit("important_greenlet died"))

Note, that you also need gevent 1.0 for this to work.

If on 0.13.6, do something like this to kill the process:

gevent.get_hub().parent.throw(SystemExit())
like image 195
Denis Avatar answered Sep 27 '22 19:09

Denis