django 在我的机器上非常慢

发布于 2024-07-10 09:10:10 字数 744 浏览 5 评论 0 原文

我全新安装了 django 1.0,并提供了一个简单的页面,需要 5 秒才能加载。 在我同事的计算机上几乎不需要时间。

我启动服务器,

python manage.py testserver

我可以看到每个 GET 请求(PNG 和样式表)大约需要半秒。

另一个奇怪的事情(我认为与之相关)是应用程序的功能测试在我的 MySQL 机器上运行速度要慢得多(比我同事的机器上慢 100 倍)。 当我将应用程序设置为使用 sqlite 时,它​​们运行得非常快。 我想说的是,sqlite 并没有太大改变加载页面所需的时间,但它确实加快了服务器的启动速度。

它看起来像 IO 问题,但我在我的机器上没有看到一般的性能问题,至少除了 django 之外。

Django运行在python2.4上,我运行的是Vista。 我还检查了python2.5。

谢谢 ΤΖΩΤΖIΟΥ,这完全是 DNS 问题,因为页面加载速度很快 我转到 http://localhost:8000/app 0.1:8000/app" rel="noreferrer">http://127.0.0.1:8000/app。

但这可能是由什么引起的呢? 我的主机文件只有两个条目:

127.0.0.1   localhost
::1         localhost

I have a fresh install of django 1.0 and a simple page served from it takes 5 secs to load. On my colleague's computer it takes almost no time.

I start the server using

python manage.py testserver

I can see each GET request (PNGs and style sheets) take about half a second.

Another weird thing, which I think is related, is that the functional tests for the app run much slower on my machine with MySQL (on order of 100 times slower than on my colleague's machine). When I set the app to use sqlite, they run pretty quickly. I would like to exclaim that sqlite doesn't much change the time it takes to load a page, but it does speed up server startup.

It looks like IO problem, but I don't see general performance problems on my machine, apart from django at least.

Django runs on python2.4, I'm running Vista. I have also checked python2.5.

Thanks ΤΖΩΤΖΙΟΥ, It must totaly be a DNS problem, because the page loads up quickly as soon as
instead of http://localhost:8000/app I go to http://127.0.0.1:8000/app.

But what could it be caused by? My host file has only two entries:

127.0.0.1   localhost
::1         localhost

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

沫离伤花 2024-07-17 09:10:10

Firefox 在某些 Windows 计算机上浏览本地主机时出现问题。 您可以通过关闭 ipv6 来解决该问题,但不建议这样做。 直接使用 127.0.0.1 是解决该问题的另一种方法。

Firefox has a problem browsing to localhost on some Windows machines. You can solve it by switching off ipv6, which isn't really recommended. Using 127.0.0.1 directly is another way round the problem.

深爱成瘾 2024-07-17 09:10:10

这些帖子都没有帮助我。 在我的具体情况下, 贾斯汀·卡莫尼给了我答案。

问题

为了便于开发,我在 /etc/hosts 文件中将 [hostname].local 映射到 127.0.0.1,并且 dns 请求需要 5 秒才能解析。 有时他们会很快解决,有时却不会。

解决方案

Apple 正在使用 .local 在较新的 Snow Leopard 版本(我想我在更新到 10.6.8 后开始注意到它)和 Mac OS X Lion 上发挥一些神奇的作用。 如果您将开发主机名更改为以 local 开头而不是以 local 结尾,那么您应该已准备就绪。 此外,您几乎可以使用本地以外的任何 TLD,并且它不会发生冲突。

示例

test.local 可能会变成:

  • local.test.com
  • test.dev
  • test.[anything but local]

并且您的主机文件条目将显示为:

local.test.com  127.0.0.1

注意:此解决方案还有一个额外的好处: [hostname].com 的子域,可以更轻松地为 Facebook API 等指定应用程序域名。

可能还需要在终端中运行 dscacheutil -flushcache 以便在之后进行良好的测量你更新/etc/hosts

None of these posts helped me. In my specific case, Justin Carmony gave me the answer.

Problem

I was mapping [hostname].local to 127.0.0.1 in my /etc/hosts file for easy development purposes and dns requests were taking 5 seconds at to resolve. Sometimes they would resolve quickly, other times they wouldn't.

Solution

Apple is using .local to do some bonjour magic on newer Snow Leopard builds (I think i started noticing it after updating to 10.6.8) and Mac OS X Lion. If you change your dev hostname to start with local instead of end with local you should be all set. Additionally, you can pretty much use any TLD besides local and it will work without conflict.

Example

test.local could become:

  • local.test.com
  • test.dev
  • test.[anything but local]

and your hosts file entry would read:

local.test.com  127.0.0.1

Note: This solution has the added benefit of being a subdomain of [hostname].com which makes it easier to specify an app domain name for Facebook APIs, etc.

Might also want to run dscacheutil -flushcache in the terminal for good measure after you update /etc/hosts

独行侠 2024-07-17 09:10:10

我过去也遇到过同样的问题。 可以通过从主机文件中删除以下行来解决此问题。

::1         localhost

一旦消失,您应该能够快速再次使用 localhost。

I have had the same problem in the past. It can be solved by removing the following line from your hosts file.

::1         localhost

Once that's gone you should be able to use localhost again, quickly.

夜清冷一曲。 2024-07-17 09:10:10

由于您报告您朋友的计算机没有延迟,并且我假设您的计算机和他的计算机具有可比性,因此这可能是与 DNS 相关的问题。 尝试将客户端和服务器的 IP 地址添加到服务器的主机文件中。

如果您在 *nix 上运行 Django,则它是 /etc/hosts 文件。 如果在 MS Windows 上运行它,则它是 %WINDIR%\SYSTEM32\DRIVERS\ETC\HOSTS 文件。 (它们是文本文件,可以用您最喜欢的文本编辑器进行编辑。)

Since you report your friend's machine has no delays, and I assume yours and his are comparable computers, it could be a DNS related issue. Try to add both the client's and the server's IP address to the server's hosts file.

If you run Django on *nix, it's the /etc/hosts file. If run it on MS Windows, it's the %WINDIR%\SYSTEM32\DRIVERS\ETC\HOSTS file. (They are text files and can be edited by your favourite text editor.)

苦笑流年记忆 2024-07-17 09:10:10

我认为这是开发服务器,它没有针对速度和安全性进行优化。 我注意到专门提供静态文件(即媒体)的速度很慢。

I think it's the development server, it's not optimized for speed nor security. I noticed that specially serving static files (i.e. media) is slow.

ι不睡觉的鱼゛ 2024-07-17 09:10:10

如果一切都失败了,您可以随时 cProfile 您的应用程序并查看到底在哪里瓶颈。

And if all else fails, you can always cProfile your application and see where exactly is the bottleneck.

被翻牌 2024-07-17 09:10:10

也有同样的问题,我在 Vista 和 Windows 7 机器上使用 Firefox 时注意到了这个问题。 访问开发服务器127.0.0.1:8000解决了问题。

Had the same problem too, I've noticed it with Firefox on Vista and Windows 7 machines. Accessing the development server 127.0.0.1:8000 solved the problem.

拍不死你 2024-07-17 09:10:10

升级到 Django 1.3 或更高版本。

如果您在 2012 年(使用 Chrome)仍然遇到此问题,请确保您正在升级。 在 1.3 中,修复了与开发服务器为单线程时速度缓慢相关的 bug

升级解决了我的问题。 我运行的是 Django 1.2,因为这是 Debian Squeeze 上的默认版本。

Upgrade to Django 1.3 or newer.

If you still have this problem in 2012 (using Chrome), make sure you're upgrading. In 1.3, a bug was fixed related to slowness when the dev server was single threaded.

Upgrading solved my issues. I was running Django 1.2 since that's the default on Debian Squeeze.

若有似无的小暗淡 2024-07-17 09:10:10

我也遇到了同样的问题,但它是由 mysqld 引起的。

该应用程序在同一主机中使用 wsgi 和 mysql 服务器在生产环境中运行得相当快,但在使用 django runserver 选项和远程 mysql 服务器的开发环境中运行缓慢。

我注意到使用“显示进程列表”,数据库中的连接陷入用户“未经身份验证的用户”的“登录”状态,这让我注意到问题出在身份验证过程中。

寻找真正的问题,我终于注意到mysqld正在尝试获取我的ip的dns名称并禁用名称dns解析,我解决了这个问题。

为此,您可以将此行添加到 my.cnf 文件中:

跳过名称解析

I had the same problem but it was caused by mysqld.

The app worked pretty fast on production with wsgi and a mysql server in the same host but slow in the development environtment with the django runserver option and a remote mysql server.

I noticed using "show processlist" that connections in database where stuck in the state "login" with user "unauthenticated user" and this make me notice that the problem where in the authentication process.

Looking for the real problem, I finally noticed that mysqld was trying to get the dns name of my ip and disabling the name dns resolution, I fixed the problem.

To do that, you can add this line into my.cnf file:

skip-name-resolve

來不及說愛妳 2024-07-17 09:10:10

我这样解决了这个问题:

转到 setting.py 并设置

DEBUG = False

I solved this issue like this:

go to setting.py and set

DEBUG = False
原谅过去的我 2024-07-17 09:10:10

禁用 AV 扫描和 看看这是否有影响。 也可能是 Vista 引起的。 升级到最新的服务包并重试。

Disable AV Scanning & see if that makes a difference. It could also be caused by Vista. Upgrade to the latest service pack and try again.

吃兔兔 2024-07-17 09:10:10

要完全绕过 localhost 而不更改 Firefox 中的主机文件或任何设置,您可以安装插件 Redirector 并制定一条从 localhost 重定向到 127.0.0.1 的规则。 使用这些设置

Include pattern : http://localhost*
Redirect to     : http://127.0.0.1$1

将其他字段留空。

To completely bypass localhost without altering the hosts file or any settings in Firefox you can install the addon Redirector and make a rule to redirect from localhost to 127.0.0.1. Use these settings

Include pattern : http://localhost*
Redirect to     : http://127.0.0.1$1

Leave the other fields empty.

渡你暖光 2024-07-17 09:10:10

我遇到了同样的问题。

解决方案是:

  • 我试图在本地启动一个通常部署在生产中的网络服务器上的版本。
  • 生产中的静态文件由 apache 配置提供,而不是由 django 静态服务提供,

所以我只是取消注释回我的静态服务网址:/

i got the same problem.

the solution was :

  • I was trying to start localy a version that was usualy deployed on a webserver in production.
  • The static files in production were served by an apache config and not with django static serve

so I just uncommented back my static serve urls :/

拥抱影子 2024-07-17 09:10:10

静态文件加载缓慢

如果您发现静态文件和媒体文件(图像、样式表等)加载速度特别慢,问题可能出在 Django 的开发服务器上(python manage.py运行服务器)。 正如 hassen 所指出的,该服务器未针对速度进行优化。

您可以尝试使用 django-extensions runserver_plus 命令与--threaded 选项替代 Django 的 runserver 命令。 在底层,它使用 Werkzeug 作为线程 WSGI 服务器。 您可能会注意到静态文件的加载时间有了巨大的改进。

另请参阅:使 Django 开发服务器更快地提供静态媒体

Slow Loading of Static Files

If you notice that static and media files (images, style sheets, etc.) are particularly slow to load, the problem might be Django's development server (python manage.py runserver). As noted by hassen, that server is not optimized for speed.

You can try using django-extensions runserver_plus command with the --threaded option as a replacement for Django's runserver command. Under the hood, it uses Werkzeug as the threaded WSGI server. You may notice a huge improvement in loading times for static files.

Also see: Making Django development server faster at serving static media

素罗衫 2024-07-17 09:10:10

对我来说,它是 Django 调试工具栏 - 当需要时它非常有用,但当不需要时它会减慢开发速度,这可能会非常令人沮丧且难以识别。 我发现最好在开发过程中禁用它,除非你需要它。 :僵尸永不死:

For me it was Django Debug Toolbar - its amazingly helpful when its needed but when it isn't it can slow development down which can be extremely frustrating and hard to identify. I find its better to disable it during development unless you need it. :zombies-never-die:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文