Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 18 fix #70

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions firebase/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
33 changes: 28 additions & 5 deletions firebase/async.py
Original file line number Diff line number Diff line change
@@ -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)