From 923f4ccd9da9d0ab49ecc4408389e186854a8314 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 1 Sep 2016 14:56:43 +0300 Subject: [PATCH] Issue 18 fix --- firebase/__init__.py | 16 +--------------- firebase/async.py | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/firebase/__init__.py b/firebase/__init__.py index 8c84e53..f62cc27 100644 --- a/firebase/__init__.py +++ b/firebase/__init__.py @@ -1,15 +1 @@ -import atexit - -from .async import process_pool -from firebase import * - - -@atexit.register -def close_process_pool(): - """ - Clean up function that closes and terminates the process pool - defined in the ``async`` file. - """ - process_pool.close() - process_pool.join() - process_pool.terminate() +from firebase import * \ No newline at end of file diff --git a/firebase/async.py b/firebase/async.py index b343d38..e0cc1c9 100644 --- a/firebase/async.py +++ b/firebase/async.py @@ -1,14 +1,37 @@ -import multiprocessing - +import threading from .lazy import LazyLoadProxy __all__ = ['process_pool'] _process_pool = None +_singleton_lock = threading.Lock() + + def get_process_pool(size=5): global _process_pool - if _process_pool is None: - _process_pool = multiprocessing.Pool(processes=size) + global _singleton_lock + + if _process_pool is not None: + return _process_pool + + # initialize process_pool thread safe way + with _singleton_lock: + if _process_pool is None: + import atexit + import multiprocessing + _process_pool = multiprocessing.Pool(processes=size) + + # atexit will work only if multiprocessing pool is initialized. + @atexit.register + def close_process_pool(): + """ + Clean up function that closes and terminates the process pool + """ + _process_pool.close() + _process_pool.join() + _process_pool.terminate() + return _process_pool -process_pool = LazyLoadProxy(get_process_pool) + +process_pool = LazyLoadProxy(get_process_pool) \ No newline at end of file