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

时间:2021-08-11 21:21:51

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. Didn't have this problem though on Snow Leopard.

即使在新创建的Django 1.4项目中也会发生这种情况。虽然在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?

但我不明白为什么?

3 个解决方案

#1


10  

(for guys still googling the answer)

(对于仍在谷歌搜索答案的人)

I had similar problem using Vagrant (on Windows host machine). Solution for me was move virtualenv folder away from synced /vagrant. Default settings of synced folders uses VirtualBox provider and that's the problem. We can read about this in another sync methods from Vagrant official documentation:

我使用Vagrant有类似的问题(在Windows主机上)。解决方案对我来说是移动virtualenv文件夹远离同步/流浪汉。同步文件夹的默认设置使用VirtualBox提供程序,这就是问题所在。我们可以从Vagrant官方文档中的另一种同步方法中了解这一点:

In some cases the default shared folder implementations (such as VirtualBox shared folders) have high performance penalties. If you're seeing less than ideal performance with synced folders, NFS can offer a solution.

在某些情况下,默认的共享文件夹实现(例如VirtualBox共享文件夹)具有很高的性能损失。如果您发现同步文件夹的性能不佳,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共享文件夹基准。

#2


1  

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. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument, as described below. http://linux.die.net/man/2/waitpid

waitpid的联机帮助页说:waitpid()系统调用暂停执行调用进程,直到pid参数指定的子项已更改状态。默认情况下,waitpid()仅等待已终止的子项,但此行为可通过options参数进行修改,如下所述。 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. Once the "worker" exits, it starts up a new worker with the new source files. The "worker" is taking a long time to exit.

所以“老板”(28374)注意到对文件的更改并告诉“工人”(7968)退出。一旦“worker”退出,它就会使用新的源文件启动一个新的worker。 “工人”需要很长时间才能退出。

Or OSX THINKS it's taking a long time to exit. 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.

或者OSX认为它需要很长时间才能退出。如果由于某种原因操作系统在内核中进行簿记并延迟更新状态,那么最终可能会出现这样的延迟。

Or perhaps there's something else entirely going on. It's perplexing.

或者也许还有其他事情在继续发生。这令人困惑。

#3


0  

In my case, it was caused by the DjangoWhiteNoise module being loaded in the wsgi.py file. After I added a condition that disabled the module in my development environment, the server reload time decreased substantially.

在我的例子中,它是由在wsgi.py文件中加载DjangoWhiteNoise模块引起的。添加了在我的开发环境中禁用模块的条件后,服务器重新加载时间大幅减少。

#1


10  

(for guys still googling the answer)

(对于仍在谷歌搜索答案的人)

I had similar problem using Vagrant (on Windows host machine). Solution for me was move virtualenv folder away from synced /vagrant. Default settings of synced folders uses VirtualBox provider and that's the problem. We can read about this in another sync methods from Vagrant official documentation:

我使用Vagrant有类似的问题(在Windows主机上)。解决方案对我来说是移动virtualenv文件夹远离同步/流浪汉。同步文件夹的默认设置使用VirtualBox提供程序,这就是问题所在。我们可以从Vagrant官方文档中的另一种同步方法中了解这一点:

In some cases the default shared folder implementations (such as VirtualBox shared folders) have high performance penalties. If you're seeing less than ideal performance with synced folders, NFS can offer a solution.

在某些情况下,默认的共享文件夹实现(例如VirtualBox共享文件夹)具有很高的性能损失。如果您发现同步文件夹的性能不佳,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共享文件夹基准。

#2


1  

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. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument, as described below. http://linux.die.net/man/2/waitpid

waitpid的联机帮助页说:waitpid()系统调用暂停执行调用进程,直到pid参数指定的子项已更改状态。默认情况下,waitpid()仅等待已终止的子项,但此行为可通过options参数进行修改,如下所述。 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. Once the "worker" exits, it starts up a new worker with the new source files. The "worker" is taking a long time to exit.

所以“老板”(28374)注意到对文件的更改并告诉“工人”(7968)退出。一旦“worker”退出,它就会使用新的源文件启动一个新的worker。 “工人”需要很长时间才能退出。

Or OSX THINKS it's taking a long time to exit. 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.

或者OSX认为它需要很长时间才能退出。如果由于某种原因操作系统在内核中进行簿记并延迟更新状态,那么最终可能会出现这样的延迟。

Or perhaps there's something else entirely going on. It's perplexing.

或者也许还有其他事情在继续发生。这令人困惑。

#3


0  

In my case, it was caused by the DjangoWhiteNoise module being loaded in the wsgi.py file. After I added a condition that disabled the module in my development environment, the server reload time decreased substantially.

在我的例子中,它是由在wsgi.py文件中加载DjangoWhiteNoise模块引起的。添加了在我的开发环境中禁用模块的条件后,服务器重新加载时间大幅减少。