简体   繁体   English

Django开发服务器重新加载需要太长时间

[英]Django development server reload takes too long

This has been my problem since I've upgraded to OSX Lion: Whenever the runserver reloads when I change a file in my Django project, it takes quite a while before it starts serving again. 这是我的问题,因为我升级到OSX Lion:每当我在Django项目中更改文件时runserver重新加载,它需要很长时间才能再次开始服务。

This happens even in a newly created Django 1.4 project. 即使在新创建的Django 1.4项目中也会发生这种情况。 Didn't have this problem though on Snow Leopard. 虽然在Snow Leopard上没有这个问题。

I used cProfile and this is where it spent most of its time: 我使用了cProfile,这是它花费大部分时间的地方:

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.001    0.001   48.068   48.068 manage.py:2(<module>)
    1    0.000    0.000   48.033   48.033 __init__.py:431(execute_manager)
    1    0.000    0.000   48.032   48.032 __init__.py:340(execute)
    1    0.000    0.000   47.908   47.908 base.py:182(run_from_argv)
    1    0.000    0.000   47.907   47.907 base.py:193(execute)
    1    0.000    0.000   47.814   47.814 runserver.py:39(handle)
    1    0.000    0.000   47.814   47.814 runserver.py:69(run)
    1    0.001    0.001   47.814   47.814 autoreload.py:129(main)
    1    0.000    0.000   47.813   47.813 autoreload.py:107(python_reloader)
    1    0.000    0.000   47.813   47.813 autoreload.py:96(restart_with_reloader)
    1    0.000    0.000   47.813   47.813 os.py:565(spawnve)
    1    0.000    0.000   47.813   47.813 os.py:529(_spawnvef)
    1   47.812   47.812   47.812   47.812 {posix.waitpid}
    ...

But I don't understand why? 但我不明白为什么?

(for guys still googling the answer) (对于仍在谷歌搜索答案的人)

I had similar problem using Vagrant (on Windows host machine). 我使用Vagrant(在Windows主机上)有类似的问题。 Solution for me was move virtualenv folder away from synced /vagrant . 解决方案对我来说是移动virtualenv文件夹远离同步/vagrant Default settings of synced folders uses VirtualBox provider and that's the problem. 同步文件夹的默认设置使用VirtualBox提供程序,这就是问题所在。 We can read about this in another sync methods from Vagrant official documentation : 我们可以通过Vagrant官方文档中的另一种同步方法来了解这一点:

In some cases the default shared folder implementations (such as VirtualBox shared folders) have high performance penalties. 在某些情况下,默认的共享文件夹实现(例如VirtualBox共享文件夹)具有高性能损失。 If you're seeing less than ideal performance with synced folders, NFS can offer a solution. 如果您发现同步文件夹的性能不佳,NFS可以提供​​解决方案。

and

SMB is built-in to Windows machines and provides a higher performance alternative to some other mechanisms such as VirtualBox shared folders. SMB是Windows机器内置的,它提供了一些其他机制(如VirtualBox共享文件夹)的更高性能替代方案。

See Vagrant shared folders benchmark for extra info. 有关额外信息,请参阅Vagrant共享文件夹基准

The manpage for waitpid says: The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. waitpid的联机帮助页说:waitpid()系统调用暂停执行调用进程,直到pid参数指定的子进程发生更改状态。 By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument, as described below. 默认情况下,waitpid()仅等待已终止的子项,但此行为可通过options参数进行修改,如下所述。 http://linux.die.net/man/2/waitpid http://linux.die.net/man/2/waitpid

Why is it taking so long for the child process to change state? 为什么儿童过程改变国家需要这么长时间? The django manage.py runserver command is a very thin wrapper over ANOTHER runserver command: django manage.py runserver命令是一个非常薄的包装器,超过了另一个runserver命令:

 2533 pts/0    Ss     0:00  \_ bash
28374 pts/0    S+     0:00  |   \_ ../env/bin/python ./manage.py runserver
 7968 pts/0    Sl+   20:26  |       \_/home/sandford/workspace/usgm_apps/usgm_apps/../env/bin/python ./manage.py runserver

So the "boss" (28374) notices a change on a file and tells the "worker" (7968) to exit. 所以“老板”(28374)注意到文件的变化并告诉“工人”(7968)退出。 Once the "worker" exits, it starts up a new worker with the new source files. 一旦“worker”退出,它就会使用新的源文件启动一个新的worker。 The "worker" is taking a long time to exit. “工人”需要很长时间才能退出。

Or OSX THINKS it's taking a long time to exit. 或者OSX认为它需要很长时间才能退出。 If the OS gets behind on bookkeeping in the kernel for some reason and delays updating state you could end up with a delay like this. 如果由于某种原因操作系统在内核中进行簿记并延迟更新状态,那么最终可能会出现这样的延迟。

Or perhaps there's something else entirely going on. 或者也许还有其他事情要发生。 It's perplexing. 这令人困惑。

In my case, it was caused by the DjangoWhiteNoise module being loaded in the wsgi.py file. 在我的例子中,它是由在wsgi.py文件中加载DjangoWhiteNoise模块引起的。 After I added a condition that disabled the module in my development environment, the server reload time decreased substantially. 在我添加了在我的开发环境中禁用模块的条件后,服务器重新加载时间大幅减少。

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

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