【问题标题】:Python Twisted DaemonPython 扭曲的守护进程
【发布时间】:2011-06-04 19:35:42
【问题描述】:

我写了一个简单的twisted server -

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 上运行良好。请注意,当我使用nohupctrl+Z 使进程在后台运行时,它在终端(前台)中运行。服务器不响应请求。我应该怎么做才能守护这个扭曲的服务器

【问题讨论】:

  • 你是在后台运行它,还是只是用 ctrl+z 暂停它?
  • 我试过ctrl+z。我还能如何去魔化它?
  • 在你的shell中输入“ctrl+z”后输入“bg”。这会将暂停的进程作为后台作业恢复

标签: python twisted twisted.web twisted.client twisted.internet


【解决方案1】:

我建议您研究一下twistd。这样您就不必担心处理任何启动、pid 文件管理等。他们网站上的文档非常好:http://twistedmatrix.com/documents/current/core/howto/basics.html。另请查看http://twistedmatrix.com/documents/current/core/howto/tap.html 了解如何实现应用程序文件。

【讨论】:

    【解决方案2】:

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

    要将其作为后台作业直接运行,请键入

    python myserver.py &
    

    要直接将其作为后台作业运行且在您注销时不会停止,请键入

    nohup python myserver.py &
    

    还要注意nohup,并不是真正的去魔化。在此处查看差异:What's the difference between nohup and a daemon?

    如果您真的想对您的 Twisted 服务器进行去魔化,最好的选择是使用 Mark Loeser 回答的 twistd

    【讨论】:

    • 谢谢。已经这样做了,它有效。由于 nmicheal 的答案不是真正的答案,因此无法投票或标记...
    • 这并不是真正的守护进程。例如,当您关闭终端窗口时,stdin、stdout 和 stderr 将简单地消失。
    • @Glyph 当然。这就是为什么我有最后一句话“但是如果你真的想去恶魔化它,请使用 Mark Loeser 回答的twistd。”我只回答了如何在后台运行或使用nohup,而不是去恶魔化它。
    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    相关资源
    最近更新 更多