Python的扭曲守护

Python的扭曲守护

问题描述:

我写了一个简单的扭曲的服务器 -Python的扭曲守护

from twisted.internet import reactor 
from twisted.internet import protocol 
from twisted.web import server, resource 
from twisted.internet import reactor 

class Index(resource.Resource): 
    isLeaf = True 
    def render_GET(self, request): 
     args = request.args 
     print 'Args: %s' %(repr(args)) 

print 'Serving on PORT: 8090' 
site = server.Site(Index()) 
reactor.listenTCP(8090, site) 
reactor.run() 

这个运行在127.0.0.1:8090罚款。请注意,这将在终端(前台)中运行,当我使用nohup & ctrl+Z在后台运行该进程时。服务器不响应请求。我应该怎么做,以守护这个扭曲的服务器

+0

你是否真的在背景中,或者只是用ctrl + z暂停它? – nmichaels 2011-01-06 17:56:58

+0

以及我试过`ctrl + z`。我该如何对它进行deamonize? – 2011-01-06 17:58:38

+3

输入“ctrl + z”后,在shell中键入“bg”。这将恢复被暂停的过程作为后台作业 – Rakis 2011-01-06 18:54:41

正如nmichael和Rakis已经提到的,在“ctrl + z”之后键入“bg”以恢复暂停过程作为后台作业。

要直接运行它作为后台作业,类型

python myserver.py & 

要直接作为后台作业运行它时,你注销不会停止,类型

nohup python myserver.py & 

还要注意的是nohup,是不是真正的deamonization。看到这里的差异:What's the difference between nohup and a daemon?

如果你真的想deamonize您的扭曲的服务器,最好的选择是使用twistd马克Loeser回答。

我建议寻找扭曲。这样你就不必担心处理任何启动,pid文件管理等。他们网站上的文档相当不错:http://twistedmatrix.com/documents/current/core/howto/basics.html。还请检查http://twistedmatrix.com/documents/current/core/howto/tap.html了解如何实施应用程序文件。