From 50bfe4c74699c61d20076feaea16a8a9cc7b45e2 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 24 Feb 2019 20:13:18 +0000 Subject: [PATCH] service: don't acquire lock when pool already initialized --- mitogen/service.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/mitogen/service.py b/mitogen/service.py index 9e17482c..eb7b856a 100644 --- a/mitogen/service.py +++ b/mitogen/service.py @@ -77,19 +77,24 @@ else: def get_or_create_pool(size=None, router=None): global _pool global _pool_pid - _pool_lock.acquire() - try: - if _pool_pid != os.getpid(): - _pool = Pool(router, [], size=size or DEFAULT_POOL_SIZE, - overwrite=True) - # In case of Broker shutdown crash, Pool can cause 'zombie' - # processes. - mitogen.core.listen(router.broker, 'shutdown', - lambda: _pool.stop(join=False)) - _pool_pid = os.getpid() - return _pool - finally: - _pool_lock.release() + + my_pid = os.getpid() + if _pool is None or my_pid != _pool_pid: + # Avoid acquiring heavily contended lock if possible. + _pool_lock.acquire() + try: + if _pool_pid != my_pid: + _pool = Pool(router, [], size=size or DEFAULT_POOL_SIZE, + overwrite=True) + # In case of Broker shutdown crash, Pool can cause 'zombie' + # processes. + mitogen.core.listen(router.broker, 'shutdown', + lambda: _pool.stop(join=True)) + _pool_pid = os.getpid() + finally: + _pool_lock.release() + + return _pool def call(service_name, method_name, call_context=None, **kwargs):