简体   繁体   English

重新启动在Apache + mod_python上运行的Django应用程序

[英]Restarting a Django application running on Apache + mod_python

I'm running a Django app on Apache + mod_python. 我在Apache + mod_python上运行Django应用程序。 When I make some changes to the code, sometimes they have effect immediately, other times they don't, until I restart Apache. 当我对代码进行一些更改时,有时它们会立即生效,有时则不会生效,直到我重新启动Apache。 However I don't really want to do that since it's a production server running other stuff too. 但是我真的不想那样做,因为它也是一个运行其他东西的生产服务器。 Is there some other way to force that? 还有其他方法可以强迫吗?

Just to make it clear, since I see some people get it wrong, I'm talking about a production environment. 为了说清楚,因为我看到有些人弄错了,我说的是生产环境。 For development I'm using Django's development server, of course. 对于开发,我当然使用Django的开发服务器。

If possible, you should switch to mod_wsgi. 如果可能,您应该切换到mod_wsgi。 This is now the recommended way to serve Django anyway, and is much more efficient in terms of memory and server resources. 现在这是为Django提供服务的推荐方式 ,并且在内存和服务器资源方面效率更高。

In mod_wsgi, each site has a .wsgi file associated with it. 在mod_wsgi中,每个站点都有一个与之关联的.wsgi文件。 To restart a site, just touch the relevant file, and only that code will be reloaded. 要重新启动站点,只需touch相关文件,只重新加载该代码。

As others have suggested, use mod_wsgi instead. 正如其他人所建议的那样,请使用mod_wsgi。 To get the ability for automatic reloading, through touching the WSGI script file, or through a monitor that looks for code changes, you must be using daemon mode on UNIX. 要通过触摸WSGI脚本文件或通过查找代码更改的监视器来获得自动重新加载的能力,您必须在UNIX上使用守护程序模式。 A slight of hand can be used to achieve same on Windows when using embedded mode. 使用嵌入式模式时,可以使用轻微的手在Windows上实现相同的功能。 All the details can be found in: 所有细节都可以在:

http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode

You can reduce number of connections to 1 by setting "MaxRequestsPerChild 1" in your httpd.conf file. 您可以通过在httpd.conf文件中设置“MaxRequestsPerChild 1”来减少连接数为1。 But do it only on test server, not production. 但是只在测试服务器上进行,而不是生产。

or 要么

If you don't want to kill existing connections and still restart apache you can restart it "gracefully" by performing "apache2ctl gracefully" - all existing connections will be allowed to complete. 如果您不想终止现有连接并仍然重新启动apache,则可以通过“优雅地”执行“apache2ctl”来“优雅地”重新启动它 - 将允许所有现有连接完成。

Use a test server included in Django. 使用Django中包含的测试服务器。 (like ./manage.py runserver 0.0.0.0:8080 ) It will do most things you would need during development. (例如./manage.py runserver 0.0.0.0:8080 )它将完成开发过程中需要的大部分工作。 The only drawback is that it cannot handle simultaneous requests with multi-threading. 唯一的缺点是它无法处理多线程的同时请求。

I've heard that there is a trick that setting Apache's max instances to 1 so that every code change is reflected immediately--but because you said you're running other services, so this may not be your case. 我听说有一个技巧可以将Apache的max实例设置为1,以便立即反映每个代码更改 - 但是因为你说你正在运行其他服务,所以这可能不是你的情况。

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

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