使用Python中的事件监视系统

时间:2017-06-21 12:35:55

标签: python python-3.x events

我正在用Python创建一个项目,我想添加一个利用事件和事件处理程序的监控系统。我希望这个系统在整个项目中都可用。我有以下操作:

  • 定义活动。该事件可以将一些数据作为参数。
  • 定义监视器。监视器注册某个事件。多个监视器可以注册同一事件。我想创建不同类型的监视器,例如一个用于打印数据,一个用于创建包含数据的图等。因此,监视器应该是一个类,能够保存它收集的所有数据,直到调用某些方法(例如print,create-log,...)。 / LI>
  • 为监视器 - 事件对定义事件处理程序。这定义了给定监视器如何响应给定事件。此操作主要是:将此数据添加到某个监视器类的实例的数据列表中。
  • 可以在事件发生时通知的通知功能。这将触发为该事件注册的所有监视器的事件处理程序。理想情况下,notify函数应该可以从项目的任何位置调用。

如何创建这样的系统?有没有可以帮助我的图书馆?我特别想知道如何以这样一种方式使这个系统在整个项目中透明化。

4 个答案:

答案 0 :(得分:2)

你可以使用四十行Python代码完成你想要的大部分工作。这是我自己一直使用的设计。选择功能名称使其成为Qt“信号”和“插槽”的直接替代品。

使用简单。您创建了PSignal。您可以通过调用connect方法注册处理程序。处理程序可以是任何可调用的。当事件发生时,您通过调用emit函数发出信号(即通知事件)。每个注册的可调用者都在该点运行。调用emit的对象不知道或关心是否有人正在倾听或者如果他们正在发生什么。

您也可以断开处理程序。

有很多调试代码,因为我发现否则某些错误很难被追踪。

在你的问题中,你希望每个处理程序都是一个监视器,而在我的设计中,处理程序只是函数。但在我看来,你的“监视器”概念独立于事件/处理程序机制。您将不得不编写函数来使您的应用程序运行,并且使这些函数调用您的监视器应该非常容易。

使用Python 3.3对代码进行了广泛测试。

#! python3
import traceback

class PSignal:
    def __init__(self, debug=False):
        self.debug = debug
        self.__handlers = []

    def clear(self):
        """Deletes all the handlers."""
        self.__handlers.clear()

    def connect(self, f):
        """f is a python function."""
        if not callable(f):
            raise ValueError("Object {!r} is not callable".format(f))
        self.__handlers.append(f)
        if self.debug:
            print("PSIGNAL: Connecting", f, self.__handlers)

    def disconnect(self, f):
        for f1 in self.__handlers:
            if f == f1:
                self.__handlers.remove(f)
                return

    def emit(self, *x, **y):
        self._emit(*x, **y)

    def check_debug(self):
        if self.debug and self.__handlers:
            print("PSIGNAL: Signal emitted")
            traceback.print_stack()

    def _emit(self, *x, **y):
        self.check_debug()
        for f in self.__handlers:
            try:
                if self.debug:
                    print("PSIGNAL: emit", f, len(x), x, y)
                f(*x, **y)
            except Exception:
                print("PSIGNAL: Error in signal", f)
                traceback.print_exc()

答案 1 :(得分:1)

查看Reactive Python(RxPy)

ReactiveX

GitHub/python

答案 2 :(得分:0)

您可以使用分布式消息传递系统(如zmq和"发布者订阅者")创建您自己的系统。图案。

我已经制作了类似构建可自定义工作流引擎的内容(Flows,https://github.com/mastro35/flows

再见 d。

答案 3 :(得分:0)

我将其用于运行状况监视,它允许用户指定回调,并允许线程监视,主动监视和被动监视:

https://gist.github.com/earonesty/4ccf8fc9bde6feac30e5c155e54dfa5f

我粘贴了下面的代码,没有进行测试(超过代码):

class MonitorInstance:
    def __init__(self, parent, label, func, threshold, active, metric):
        self.parent = parent
        self.label = label
        self.func = func
        self.threshold = threshold
        self.active = active
        self.metric = metric
        self.__errors = None

    def ok(self):
        if self.__errors is None or self.__errors:
            self.parent._ok(self)
        self.__errors = 0
        if self.metric:
            self.metric.set(0)

    def error(self):
        if not self.__errors:
            self.parent._error(self)

        if self.__errors is None:
            self.__errors = 0

        self.__errors += 1

        if self.metric:
            self.metric.inc()

    def check(self):
        try:
            self.func()
            self.ok()
        except Exception as e:
            log.error("%s error: %s", self.label, e)
            self.error()

    @property
    def healthy(self):
        return self.__errors < self.threshold

DEFAULT_THRESHOLD = 1           # errors to cause fault
DEFAULT_CHECKSECS = 5           # time in secs between checks

class Monitor:
    def __init__(self, health_callback=None, check_secs=DEFAULT_CHECKSECS, use_thread=False):
        self.active = []        # active moniors
        self.alerts = set()     # thresholds currently triggered (not healthy)
        self.health_callback = health_callback
        self.healthy = False    # default: not healthy unless a monitor is added!
        self.check_secs = check_secs
        self.last_check = 0

        if use_thread:
            assert self.check_secs > 0, "threads need to sleep"
            threading.Thread(target=self._thread_loop, daemon=True).start()

    def add(self, label, check, threshold=DEFAULT_THRESHOLD, active=False, metric=None):
        inst = MonitorInstance(self, label, check, threshold, active, metric)
        if active:
            self.active.append(inst)
        inst.check()
        return inst

    def _error(self, inst):
        self.alerts.add(inst)
        if self.healthy:
            self._callback(False)
        self.healthy = False

    def _thread_loop(self):
        while True:
            self.check()
            time.sleep(self.check_secs)

    def _callback(self, value):
        if not self.health_callback is None:
            try:
                self.health_callback(value)
            except:
                # health callback should always succeed!
                log.exception("deadlyexes: error calling %s", self.health_callback)

    def _ok(self, inst):
        self.alerts.discard(inst)
        if not self.healthy and not self.alerts:
            self._callback(True)
            self.healthy = True

    def check(self, force=False):
        if not force and (time.time() < (self.last_check + self.check_secs)):
            return False

        # returns true if check was done
        checked=False
        # convert to list prevents modifying iterators
        for inst in list(self.alerts) + self.active:
            try:
                checked=True
                inst.check()
            except:
                pass
        return checked
相关问题