Skip to content

Commit 1e4d4ad

Browse files
authored
Merge pull request #36 from HBS-HBX/#35_open_multiprocessing_log_in_context_handler
closes #35 open multiprocessing log in context handler
2 parents eb433b9 + edfee88 commit 1e4d4ad

File tree

6 files changed

+75
-4
lines changed

6 files changed

+75
-4
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
Changelog
22
---------
33

4+
0.7.5 (2018-08-20)
5+
~~~~~~~~~~~~~~~~~~
6+
* fix `#35 open multiprocessing log in context handler
7+
<https://github.com/HBS-HBX/django-elastic-migrations/issues/35>`_
8+
49
0.7.4 (2018-08-15)
510
~~~~~~~~~~~~~~~~~~
6-
* fix #33 error when nothing to resume using --resume
11+
* fix `#33 error when nothing to resume using --resume
12+
<https://github.com/HBS-HBX/django-elastic-migrations/issues/33>`_
713

814
0.7.3 (2018-08-14)
915
~~~~~~~~~~~~~~~~~~

django_elastic_migrations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django_elastic_migrations.utils import loading
1111
from django_elastic_migrations.utils.django_elastic_migrations_log import get_logger
1212

13-
__version__ = '0.7.4'
13+
__version__ = '0.7.5'
1414

1515
default_app_config = 'django_elastic_migrations.apps.DjangoElasticMigrationsConfig' # pylint: disable=invalid-name
1616

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
from __future__ import (absolute_import, division, print_function, unicode_literals)
2+
23
import logging
4+
35
from multiprocessing_logging import install_mp_handler
46

57

6-
install_mp_handler()
8+
mp_logging_enabled = False
79

810

911
def get_logger(name="django_elastic_migrations"):
1012
real_logger = logging.getLogger(name)
1113
for level in ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG']:
1214
setattr(real_logger, level, getattr(logging, level))
1315
return real_logger
16+
17+
18+
def start_multiprocessing_logging():
19+
"""
20+
start multiprocessing-logging to make each handler a synchronized queue.
21+
Called by django_elastic_migrations.utils.multiprocessing_utils.DjangoMultiProcess.__enter__
22+
See https://github.com/jruere/multiprocessing-logging/blob/master/multiprocessing_logging.py
23+
TBD: uninstall mp_logging; currently we don't stop and reset mp logging; it's a one-shot for the process.
24+
:return:
25+
:rtype: None
26+
"""
27+
global mp_logging_enabled
28+
29+
if not mp_logging_enabled:
30+
mp_logging_enabled = True
31+
install_mp_handler()

django_elastic_migrations/utils/multiprocessing_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from django.db import reset_queries
77

8+
from django_elastic_migrations.utils.django_elastic_migrations_log import start_multiprocessing_logging
9+
810
try:
911
# python 2
1012
# noinspection PyCompatibility
@@ -32,7 +34,6 @@
3234
https://engineering.talentpair.com/django-multiprocessing-153dbcf51dab
3335
"""
3436

35-
3637
USE_ALL_WORKERS = 999
3738

3839

@@ -185,6 +186,7 @@ def __init__(self, num_workers=None, max_workers=None, log_debug_info=False, sta
185186
self.queue = None
186187

187188
def __enter__(self):
189+
start_multiprocessing_logging()
188190
close_service_connections()
189191
return self
190192

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.test import TestCase
2+
3+
from django_elastic_migrations.utils import django_elastic_migrations_log
4+
5+
6+
class TestDEMLog(TestCase):
7+
8+
def test_mp_logging_setup(self):
9+
django_elastic_migrations_log.start_multiprocessing_logging()
10+
11+
self.assertTrue(django_elastic_migrations_log.mp_logging_enabled)

tests/test_multiprocessing_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from multiprocessing import cpu_count
2+
3+
from django.test import TestCase
4+
5+
from django_elastic_migrations.utils.multiprocessing_utils import DjangoMultiProcess
6+
7+
8+
def add_1(num):
9+
return { 'job_id': num, 'result': num + 1 }
10+
11+
12+
class TestMultiprocessingUtils(TestCase):
13+
14+
def test_basic_multiprocessing(self):
15+
"""
16+
Do a basic test of DjangoMultiProcess that doesn't touch the database
17+
:return:
18+
:rtype:
19+
"""
20+
21+
one_to_ten = range(1, 10)
22+
23+
workers = cpu_count()
24+
django_multiprocess = DjangoMultiProcess(workers, log_debug_info=3)
25+
26+
with django_multiprocess:
27+
django_multiprocess.map(add_1, one_to_ten)
28+
29+
results = django_multiprocess.results()
30+
31+
for result_obj in results:
32+
job_id = result_obj.get('job_id')
33+
result = result_obj.get('result')
34+
self.assertEqual(job_id + 1, result)

0 commit comments

Comments
 (0)