python django 在我的机器上很慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/362808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django is very slow on my machine
提问by luntain
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.
我有一个全新安装的 django 1.0 和一个简单的页面,从它加载需要 5 秒。在我同事的电脑上几乎不需要时间。
I start the server using
我使用启动服务器
python manage.py testserver
I can see each GET request (PNGs and style sheets) take about half a second.
我可以看到每个 GET 请求(PNG 和样式表)大约需要半秒钟。
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.
我认为相关的另一件奇怪的事情是,应用程序的功能测试在我使用 MySQL 的机器上运行得慢得多(比在我同事的机器上慢 100 倍)。当我将应用程序设置为使用 sqlite 时,它们运行得非常快。我想说,sqlite 并没有改变加载页面所需的时间,但它确实加快了服务器启动的速度。
It looks like IO problem, but I don't see general performance problems on my machine, apart from django at least.
看起来像是 IO 问题,但至少除了 django 之外,我在我的机器上没有看到一般的性能问题。
Django runs on python2.4, I'm running Vista. I have also checked python2.5.
Django 运行在 python2.4 上,我运行的是 Vista。我也检查了python2.5。
Thanks ΤΖΩΤΖΙΟΥ, It must totaly be a DNS problem, because the page loads up quickly as soon as instead of http://localhost:8000/appI go to http://127.0.0.1:8000/app.
谢谢 ΤΖΩΤΖΙΟΥ,这一定是 DNS 问题,因为页面加载速度很快,而不是http://localhost:8000/app我转到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
回答by fuzzyman
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.
Firefox 在某些 Windows 机器上浏览本地主机时出现问题。您可以通过关闭 ipv6 来解决它,这不是真正推荐的。直接使用 127.0.0.1 是解决问题的另一种方法。
回答by Tyler Brock
None of these posts helped me. In my specific case, Justin Carmonygave me the answer.
这些帖子都没有帮助我。在我的具体案例中,Justin Carmony给了我答案。
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.
为了便于开发,我在我的 /etc/hosts 文件中将 [hostname].local 映射到 127.0.0.1,并且 dns 请求需要 5 秒才能解决。有时他们会很快解决,有时他们不会。
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.
Apple 正在使用 .local 在较新的 Snow Leopard 版本(我想我在更新到 10.6.8 后开始注意到它)和 Mac OS X Lion 上做一些 bonjour 魔法。如果您将开发主机名更改为以 local 开头而不是以 local 结尾,则应该全部设置完毕。此外,您几乎可以使用除本地之外的任何 TLD,并且不会发生冲突。
Example
例子
test.local could become:
test.local 可以变成:
- local.test.com
- test.dev
- test.[anything but local]
- local.test.com
- 测试开发
- 测试。[除了本地之外的任何东西]
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.
注意:此解决方案具有作为 [hostname].com 的子域的额外好处,这使得为 Facebook API 等指定应用程序域名变得更加容易。
Might also want to run dscacheutil -flushcache
in the terminal for good measure after you update /etc/hosts
dscacheutil -flushcache
更新 /etc/hosts 后,可能还想在终端中运行以进行良好的测量
回答by Ty.
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.
一旦它消失了,您应该能够很快再次使用 localhost。
回答by tzot
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.
由于您报告您朋友的机器没有延迟,并且我假设您和他的计算机是可比较的计算机,因此可能是与 DNS 相关的问题。尝试将客户端和服务器的 IP 地址添加到服务器的主机文件中。
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.)
如果你在 *nix 上运行 Django,它就是/etc/hosts
文件。如果在 MS Windows 上运行它,它就是%WINDIR%\SYSTEM32\DRIVERS\ETC\HOSTS
文件。(它们是文本文件,可以由您最喜欢的文本编辑器进行编辑。)
回答by Dylan Bauer
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.
也有同样的问题,我注意到它与 Vista 和 Windows 7 机器上的 Firefox。访问开发服务器 127.0.0.1:8000 解决了问题。
回答by hasen
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.
我认为它是开发服务器,它没有针对速度和安全性进行优化。我注意到专门提供静态文件(即媒体)很慢。
回答by Albert Sellarès
I had the same problem but it was caused by mysqld.
我有同样的问题,但它是由 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.
该应用程序在同一主机中使用 wsgi 和 mysql 服务器在生产中运行得非常快,但在使用 django runserver 选项和远程 mysql 服务器的开发环境中运行缓慢。
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.
我注意到使用“show processlist”,数据库中的连接卡在用户“未经身份验证的用户”的“登录”状态,这让我注意到身份验证过程中的问题。
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.
寻找真正的问题,我终于注意到mysqld试图获取我ip的dns名称并禁用名称dns解析,我解决了问题。
To do that, you can add this line into my.cnf file:
为此,您可以将此行添加到 my.cnf 文件中:
skip-name-resolve
跳过名称解析
回答by Jonatan Littke
Upgrade to Django 1.3 or newer.
升级到 Django 1.3 或更新版本。
If you still have this problem in 2012 (using Chrome), make sure you're upgrading. In 1.3, a bugwas fixed related to slowness when the dev server was single threaded.
如果您在 2012 年(使用 Chrome)仍然遇到此问题,请确保您正在升级。在 1.3 中,当开发服务器为单线程时,修复了与缓慢相关的错误。
Upgrading solved my issues. I was running Django 1.2 since that's the default on Debian Squeeze.
升级解决了我的问题。我正在运行 Django 1.2,因为这是 Debian Squeeze 的默认设置。
回答by kmelvn
回答by seanyboy
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.
禁用 AV 扫描并查看是否有所不同。它也可能是由 Vista 引起的。升级到最新的服务包,然后重试。