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.
This commit is contained in:
Tim Abbott 2018-08-08 12:28:43 -07:00
parent 704967faa4
commit ec9f6702d8
2 changed files with 20 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import sys import sys
import types
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR) 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.") print("Error accessing Zulip secrets; manage.py in production must be run as the zulip user.")
sys.exit(1) 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") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
from django.conf import settings from django.conf import settings
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line

View File

@ -15,11 +15,21 @@ framework.
""" """
import os import os
import sys import sys
import types
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import 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") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
import django import django
django.setup() # We need to call setup to load applications. django.setup() # We need to call setup to load applications.