Django Web服务器在启动守护进程后挂起 [英] Django web server hangs after it started a daemon
问题描述
我使用Django开发服务器启动一个守护进程,它执行来自views.py的所有命令,但网页挂起。守护进程正常启动,但网页挂起需要修复。我在Red Hat Enterprise Linux 6.3下工作。
为了确保这不是我或我的守护进程错误,我执行了以下测试:
1)我创建了新的Django项目djtesting,其中创建了一个具有以下代码的view.py文件(它启动httpd守护进程):
from django.http import HttpResponse
import subprocess
def hello(request):
res = subprocess.call(/ usr / sbin / httpd)
返回HttpResponse(测试)
2)添加此功能来自djtesting的url.py:
从django.conf.urls.defaults导入模式,include,url
/ pre>
。视图import hello
urlpatterns = patterns('',
('^ hello / $',hello),
)
3)然后我用python manage.py runserver 192.168.1.226:8000启动Web服务器,并打开带有 http:// 1 92.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下工作,我不能使用start-stop-daemon:
为什么挂起网页上启动了一个守护进程底层服务器?解决方案
subprocess.call
等待一个返回值,所以我很惊讶,你可以得到一个返回值。尝试使用subprocess.Popen
,因为产生该过程,然后将控制权返回给您,而不是等待结束。I use Django develpment server to start a daemon, it performs all the commands from the views.py, but web page hangs. Daemon is starting normally but web page hanging needs to be fixed. I work under Red Hat Enterprise Linux 6.3.
To make sure that it's not my or my daemon mistake I performed the following test:
1) I created new Django project "djtesting", where created one views.py file with the following code (it starts httpd daemon):
from django.http import HttpResponse import subprocess def hello(request): res = subprocess.call("/usr/sbin/httpd") return HttpResponse("Testing.")
2) Added this function to urls.py:
from django.conf.urls.defaults import patterns, include, url from djtesting.views import hello urlpatterns = patterns('', ('^hello/$', hello), )
3) Then I started web server with "python manage.py runserver 192.168.1.226:8000" and open web page with "http://192.168.1.226:8000/hello/" in browser. It shows "Testing" message and then hangs (starts loading and hangs on this) although daemon started normally. But if to stop the daemon with "/etc/init.d/httpd stop" the web page stops loading. It seems that the server is waiting until the daemon finish working, but I need just to start it without waiting until it ends.
I've tried another ways to run a daemon (one line per one attempt of course) but with the same poor result:
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', '&')
I've found the similar question but I cannot use start-stop-daemon since I work under RHEL6.3: Why hangs the web page after it started a daemon on the underlying server?
解决方案
subprocess.call
waits for a return value, so I'm rather surprised you're able to get a return value at all. Try usingsubprocess.Popen
instead, as that spawns the process and then returns control back to you rather than waiting for the end.这篇关于Django Web服务器在启动守护进程后挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!