Log.error("%s error: %s", self.label, healthy(self):
I pasted the code below, without the tests (more than the code): class MonitorInstance:ĭef _init_(self, parent, label, func, threshold, active, metric): I use this one for health monitoring, which allows the user to specify callbacks, and allows for both threaded, active monitors, and passive monitors: Print("PSIGNAL: Connecting", f, self._handlers) Raise ValueError("Object is not callable".format(f))
The code is extensively tested with Python 3.3. You're going to have to write functions to make your application go, and it should be pretty easy to make those functions call your monitors. But it seems to me that your "monitor" concept is independent of the event/handler mechanism. In your question you wanted each handler to be a monitor, and in my design handlers are just functions. There is a lot of debugging code because I discovered that otherwise certain errors can be difficult to track down. The object calling emit doesn't know, or care, whether anyone is listening or what happens if they are. Every registered callable runs at that point. When an event occurs you emit a signal (i.e., notify an event) by calling the emit function. You register handlers by calling the connect method. The function names are chosen to make it a drop-in replacement for Qt's "signals" and "slots". This is my own design that I use all the time. You can do most of what you want with forty lines of Python code. How can I create such a system? Are there any libraries that can help me with this? I am especially wondering how I can make this system in such a way that it is transparently available throughout the project. Ideally, the notify function should be callable from anywhere in the project. This will trigger the event-handler for all monitors that are registered for that event.