From ebfbc796ff8cd6a091b3bfcc28627370e11f90f6 Mon Sep 17 00:00:00 2001 From: andruskutt Date: Fri, 14 Jan 2022 07:49:53 +0200 Subject: [PATCH] broker: calculate signal handler name only once for each emit (#462) --- dramatiq/broker.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dramatiq/broker.py b/dramatiq/broker.py index b997aa98..c24906ef 100644 --- a/dramatiq/broker.py +++ b/dramatiq/broker.py @@ -84,20 +84,22 @@ def __init__(self, middleware=None): self.add_middleware(m) def emit_before(self, signal, *args, **kwargs): + signal = "before_" + signal for middleware in self.middleware: try: - getattr(middleware, "before_" + signal)(self, *args, **kwargs) + getattr(middleware, signal)(self, *args, **kwargs) except MiddlewareError: raise except Exception: - self.logger.critical("Unexpected failure in before_%s of %r.", signal, middleware, exc_info=True) + self.logger.critical("Unexpected failure in %s of %r.", signal, middleware, exc_info=True) def emit_after(self, signal, *args, **kwargs): + signal = "after_" + signal for middleware in reversed(self.middleware): try: - getattr(middleware, "after_" + signal)(self, *args, **kwargs) + getattr(middleware, signal)(self, *args, **kwargs) except Exception: - self.logger.critical("Unexpected failure in after_%s of %r.", signal, middleware, exc_info=True) + self.logger.critical("Unexpected failure in %s of %r.", signal, middleware, exc_info=True) def add_middleware(self, middleware, *, before=None, after=None): """Add a middleware object to this broker. The middleware is