简体   繁体   English

带有Django的Google Cloud SQL-极慢的连接

[英]Google Cloud SQL w/ Django - Extremely Slow Connection

Edit: 编辑:

After doing some further investigation, the delay seems to be more Django than the Cloud SQL Proxy. 经过进一步调查后,延迟似乎比Django多了Cloud Cloud Proxy。

I added a couple of print statements at the start and end of a view, and they print instantly when the request is made, but it takes a further 60 seconds for the page to load. 我在视图的开始和结束处添加了一些打印语句,它们在发出请求时即刻打印,但是页面加载还需要60秒。

I've stripped back the template files to include only the bare bones, removing most scripts and static resources and it's still pretty similar. 我已经剥离了模板文件,只包括裸露的骨骼,删除了大多数脚本和静态资源,并且仍然非常相似。

Changing my view to return a simple HttpResponse('Done') cuts the time drastically. 更改我的视图以返回简单的HttpResponse('Done')大大节省时间。

Whilst developing locally I am using Django to serve the static files as described in the docs . 在本地开发时,我使用Django来提供docs中所述的静态文件。 Again, I don't have this issue with other projects though. 同样,其他项目也没有这个问题。

Original Post: 原始帖子:

I've recently noticed my Django application is incredibly slow to connect to my Google Cloud SQL database when using the Cloud SQL Proxy in my local development environment. 我最近注意到,在本地开发环境中使用Cloud SQL代理时,我的Django应用程序连接到Google Cloud SQL数据库的速度异常慢。

The initial connection takes 2-3 minutes, then 60 seconds per request thereafter. 初始连接需要2-3分钟,然后每个请求需要60秒。 This applies when performing migrations or running the development server. 这在执行迁移或运行开发服务器时适用。 Eventually the request completes. 最终,请求完成。

I've tried scaling up the database but to no effect (it's relatively small anyway). 我尝试扩大数据库规模,但没有任何效果(无论如何,它相对较小)。 Database version is MySQL 5.7 with machine type db-n1-standard-1. 数据库版本为MySQL 5.7,机器类型为db-n1-standard-1。 Previously I've used Django Channels but have since removed all references to this. 以前,我曾使用Django Channels,但此后删除了所有对此的引用。

The Middleware and settings.py are relatively standard and identical to another Django app that connects in an instant. Middleware和settings.py是相对标准的,与即时连接的另一个Django应用相同。

The live site also connects very fast without any issues. 实时站点连接也非常快速,没有任何问题。

Python version is 3.6 w/ Django 2.1.4 and mysqlclient 1.3.14. Python版本是3.6 w / Django 2.1.4和mysqlclient 1.3.14。

My database settings are defined as: 我的数据库设置定义为:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'PORT': '3306',
    }
}

DATABASES['default']['HOST'] = os.getenv('DB_HOST')
if os.getenv('GAE_INSTANCE'):
    pass
else:
    DATABASES['default']['HOST'] = '127.0.0.1'

Using environment variables or not doesn't seem to make a difference. 是否使用环境变量似乎没有什么不同。

I'm starting the Cloud SQL Proxy via ./cloud_sql_proxy -instances="my-project:europe-west1:my-project-instance"=tcp:3306 . 我正在通过./cloud_sql_proxy -instances="my-project:europe-west1:my-project-instance"=tcp:3306启动Cloud SQL代理。

After invoking the proxy via the command line I see Ready for new connections . 通过命令行调用代理后,我看到“ Ready for new connections Running python manage.py runserver shows New connection for "my-project:europe-west1:my-project-instance" but then takes an age before I see Starting development server at http://127.0.0.1:8000/ . 运行python manage.py runserver显示New connection for "my-project:europe-west1:my-project-instance"但是要花一些时间才能看到Starting development server at http://127.0.0.1:8000/

I'm also noticing several errors in Stackdriver: 我还注意到Stackdriver中的几个错误:

  • _mysql_exceptions.OperationalError: (2006, "Lost connection to MySQL server at 'reading initial communication packet', system error: 95")
  • django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 95")
  • AttributeError: 'SessionStore' object has no attribute '_session_cache'

These appear - or don't - from time to time without changing any settings. 这些内容会不时显示-或不显示-而无需更改任何设置。

I've read they may be an access rights issue but the connection is eventually made, it's just incredibly slow. 我读过它们可能是访问权限问题,但最终建立了连接,速度非常慢。 I'm authorising via the Google Cloud SDK, which seems to work fine. 我正在通过Google Cloud SDK进行授权,这似乎很好。

Eventually I found that the main source of the delay was a recursive function being called in one of my admin forms (which delayed the initial startup) and context processors (which delayed each load). 最终,我发现延迟的主要来源是在我的一种管理形式(延迟了初始启动)和上下文处理器(延迟了每次加载)中调用了一个递归函数。 After removing it, the pages loaded without issue. 删除后,页面加载没有问题。 It worked fine when deployed to App Engine or when using a test/local SQLite database, though, which is what made debugging a little harder. 不过,在将其部署到App Engine或使用测试/本地SQLite数据库时,它运行良好,这使调试变得更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM