Django Web服务器在启动守护进程后挂起

时间:2012-10-10 08:30:41

标签: python django web

我使用Django develpment服务器启动一个守护进程,它执行来自views.py的所有命令,但网页挂起。守护进程正常启动但需要修复网页挂起。我在Red Hat Enterprise Linux 6.3下工作。

为了确保这不是我或我的守护进程错误,我进行了以下测试:

1)我创建了新的Django项目“djtesting”,其中创建了一个带有以下代码的一个views.py文件(它启动了httpd守护进程):

from django.http import HttpResponse
import subprocess

def hello(request):
    res = subprocess.call("/usr/sbin/httpd")
    return HttpResponse("Testing.")

2)将此功能添加到urls.py:

from django.conf.urls.defaults import patterns, include, url
from djtesting.views import hello

urlpatterns = patterns('',
    ('^hello/$', hello),
    )

3)然后我用“python manage.py runserver 192.168.1.226:8000”启动了web服务器,并在浏览器中打开了带有“http://192.168.1.226:8000/hello/”的网页。它显示“测试”消息,然后挂起(开始加载并挂起),尽管守护程序正常启动。但是如果用“/etc/init.d/httpd stop”停止守护进程,网页就会停止加载。似乎服务器正在等待守护程序完成工作,但我只需要启动它而不等到它结束。

我尝试了另一种运行守护进程的方法(当然每次尝试一行)但结果相同但是:

thread.start_new_thread(os.system, ('/usr/sbin/httpd',))
process = subprocess.Popen("/usr/sbin/httpd", stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
res = subprocess.call(["/usr/sbin/httpd", "&"])
res = subprocess.Popen("/usr/sbin/httpd")
res = os.system("/usr/sbin/httpd &")
res = os.spawnl(os.P_NOWAITO, '/usr/sbin/httpd', '&')

我发现了类似的问题但我不能使用start-stop-daemon,因为我在RHEL6.3下工作: Why hangs the web page after it started a daemon on the underlying server?

1 个答案:

答案 0 :(得分:0)

subprocess.call等待返回值,所以我很惊讶你能够获得返回值。请尝试使用subprocess.Popen,因为它会生成进程,然后将控制权返回给您,而不是等待结束。

相关问题