From ec9f6702d8d13eea45cd486fe8bc08ec56e61133 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 8 Aug 2018 12:28:43 -0700 Subject: [PATCH] manage.py: Sabotage importing pika.adapters.twisted_connection. This is a performance optimization; see the comment. This fixes part of #9953. Eventually, we should do the same thing for importing Tornado as well, but it's less important because Tornado is a much smaller library. --- manage.py | 10 ++++++++++ zproject/wsgi.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/manage.py b/manage.py index bc7b310077..83164e8190 100755 --- a/manage.py +++ b/manage.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import os import sys +import types BASE_DIR = os.path.dirname(os.path.abspath(__file__)) sys.path.append(BASE_DIR) @@ -21,6 +22,15 @@ if __name__ == "__main__": print("Error accessing Zulip secrets; manage.py in production must be run as the zulip user.") sys.exit(1) + # Performance Hack: We make the pika.adapters.twisted_connection + # module unavailable, to save ~100ms of import time for most Zulip + # management commands for code we don't use. The correct + # long-term fix for this will be to get a setting integrated + # upstream to disable pika importing this. + # See https://github.com/pika/pika/issues/1128 + sys.modules['pika.adapters.twisted_connection'] = types.ModuleType( + 'pika.adapters.twisted_connection') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings") from django.conf import settings from django.core.management import execute_from_command_line diff --git a/zproject/wsgi.py b/zproject/wsgi.py index 9fbbafc561..b00e13ee3a 100644 --- a/zproject/wsgi.py +++ b/zproject/wsgi.py @@ -15,11 +15,21 @@ framework. """ import os import sys +import types BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) import scripts.lib.setup_path_on_import +# Performance Hack: We make the pika.adapters.twisted_connection +# module unavailable, to save ~100ms of import time for most Zulip +# management commands for code we don't use. The correct +# long-term fix for this will be to get a setting integrated +# upstream to disable pika importing this. +# See https://github.com/pika/pika/issues/1128 +sys.modules['pika.adapters.twisted_connection'] = types.ModuleType( + 'pika.adapters.twisted_connection') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings") import django django.setup() # We need to call setup to load applications.