我使用Django开发服务器启动了一个守护进程,它执行来自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', '&')
我发现了类似的问题,但我不能使用启动-停止-守护进程,因为我在RHEL6.3下工作:Why hangs the web page after it started a daemon on the underlying server?
发布于 2012-10-15 22:34:16
subprocess.call
等待返回值,所以我很惊讶你能得到返回值。尝试使用subprocess.Popen
,因为这会产生进程,然后将控制权返回给您,而不是等待结束。
https://stackoverflow.com/questions/12815133
复制相似问题