From e93762b3db65411efb3e6dcb21b4d4525e91344d Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 25 Feb 2019 17:45:27 +0000 Subject: [PATCH] service: avoid taking another lock in the usual case --- mitogen/service.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/mitogen/service.py b/mitogen/service.py index eb7b856a..49bd2281 100644 --- a/mitogen/service.py +++ b/mitogen/service.py @@ -547,15 +547,18 @@ class Pool(object): invoker.service.on_shutdown() def get_invoker(self, name, msg): - self._lock.acquire() - try: - invoker = self._invoker_by_name.get(name) - if not invoker: - service = self._activator.activate(self, name, msg) - invoker = service.invoker_class(service=service) - self._invoker_by_name[name] = invoker - finally: - self._lock.release() + invoker = self._invoker_by_name.get(name) + if invoker is None: + # Avoid acquiring lock if possible. + self._lock.acquire() + try: + invoker = self._invoker_by_name.get(name) + if not invoker: + service = self._activator.activate(self, name, msg) + invoker = service.invoker_class(service=service) + self._invoker_by_name[name] = invoker + finally: + self._lock.release() return invoker