Skip to content

Commit

Permalink
broker: calculate signal handler name only once for each emit (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
andruskutt authored Jan 14, 2022
1 parent bb4cedd commit ebfbc79
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions dramatiq/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ebfbc79

Please sign in to comment.