Skip to content
Merged
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
32 changes: 20 additions & 12 deletions django_tenants/migration_executors/multiproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
from .base import MigrationExecutor, run_migrations


def get_pool():
"""Return a multiprocessing pool using the ``fork`` start method when available.

The migration workers rely on inheriting the parent process's already
populated Django app registry and settings, which only happens with the
``fork`` start method. Python no longer guarantees ``fork`` as the implicit
default (it is deprecated when the parent is multi-threaded), so request it
explicitly on platforms that support it (e.g. Linux) and fall back to the
default context elsewhere (e.g. Windows, which has no ``fork``).
"""
processes = getattr(settings, 'TENANT_MULTIPROCESSING_MAX_PROCESSES', 2)
if 'fork' in multiprocessing.get_all_start_methods():
context = multiprocessing.get_context('fork')
else:
context = multiprocessing.get_context()
return context.Pool(processes=processes)


def run_migrations_percent(args, options, codename, count, idx_schema_name):
idx, schema_name = idx_schema_name
return run_migrations(
Expand Down Expand Up @@ -44,11 +62,6 @@ def run_migrations(self, tenants=None):
tenants.pop(tenants.index(self.PUBLIC_SCHEMA_NAME))

if tenants:
processes = getattr(
settings,
'TENANT_MULTIPROCESSING_MAX_PROCESSES',
2
)
chunks = getattr(
settings,
'TENANT_MULTIPROCESSING_CHUNKS',
Expand All @@ -68,7 +81,7 @@ def run_migrations(self, tenants=None):
self.codename,
len(tenants)
)
p = multiprocessing.Pool(processes=processes)
p = get_pool()
p.map(
run_migrations_p,
enumerate(tenants),
Expand All @@ -77,11 +90,6 @@ def run_migrations(self, tenants=None):

def run_multi_type_migrations(self, tenants):
tenants = tenants or []
processes = getattr(
settings,
'TENANT_MULTIPROCESSING_MAX_PROCESSES',
2
)
chunks = getattr(
settings,
'TENANT_MULTIPROCESSING_CHUNKS',
Expand All @@ -101,7 +109,7 @@ def run_multi_type_migrations(self, tenants):
self.codename,
len(tenants)
)
p = multiprocessing.Pool(processes=processes)
p = get_pool()
p.map(
run_migrations_p,
enumerate(tenants),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires = [ "setuptools>=61" ]

[project]
name = "django-tenants"
version = "3.10.1"
version = "3.10.2"
description = "Tenant support for Django using PostgreSQL schemas."
readme = "README.rst"
license = "MIT"
Expand Down