django项目扭曲并运行为“守护进程”

时间:2011-02-15 17:17:10

标签: django twisted daemon

最近两天我正试图找到一种在扭曲的情况下运行django项目的方法。详细搜索后,我发现了几种配置它的方法。但是大多数都是处理如何通过命令行运行应用程序而不是守护进程。我想将django项目作为守护进程运行。

我尝试了以下链接来实现这一点,

  1. Twisted: Creating a ThreadPool and then daemonizing leads to uninformative hangs

  2. http://www.robgolding.com/blog/2011/02/05/django-on-twistd-web-wsgi-issue-workaround/

  3. 但是这对我来说也不起作用。通过这种方法,TCP服务器甚至不会监听给定的端口。

    请帮我弄清楚。

    更新

    对不起失踪的信息感到抱歉。这就是我的目标。

    我是扭曲世界中的初学者,所以首先我要尝试将我的工作django项目配置为扭曲,目前它在django测试服务器或通过mod_wsgi的apache上运行良好。

    要使用下面给出的参考代码,我使用下面给出的参考代码,该代码是我在第一篇文章中给出的链接中找到的两个样本的组合。

    因此,为了将django app与twisted集成,我使用了以下代码,文件名:“server.py”。

    import sys
    import os
    
    from twisted.application import internet, service
    from twisted.web import server, resource, wsgi, static
    from twisted.python import threadpool
    from twisted.internet import reactor
    from django.conf import settings
    import twresource # This file hold implementation of "Class Root".
    
    
    class ThreadPoolService(service.Service):
        def __init__(self, pool):
            self.pool = pool
    
        def startService(self):
            service.Service.startService(self)
            self.pool.start()
    
        def stopService(self):
            service.Service.stopService(self)
            self.pool.stop()
    
    class Root(resource.Resource):
        def __init__(self, wsgi_resource):
            resource.Resource.__init__(self)
            self.wsgi_resource = wsgi_resource
    
        def getChild(self, path, request):
            path0 = request.prepath.pop(0)
            request.postpath.insert(0, path0)
            return self.wsgi_resource
    
    PORT = 8080
    
    # Environment setup for your Django project files:
    #insert it to first so our project will get first priority.
    sys.path.insert(0,"django_project")
    sys.path.insert(0,".")
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
    from django.core.handlers.wsgi import WSGIHandler
    
    def wsgi_resource():
        pool = threadpool.ThreadPool()
        pool.start()
        # Allow Ctrl-C to get you out cleanly:
        reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
        wsgi_resource = wsgi.WSGIResource(reactor, pool, WSGIHandler())
        return wsgi_resource
    
    
    # Twisted Application Framework setup:
    application = service.Application('twisted-django')
    
    # WSGI container for Django, combine it with twisted.web.Resource:
    # XXX this is the only 'ugly' part: see the 'getChild' method in twresource.Root
    
    wsgi_root = wsgi_resource()
    root = Root(wsgi_root)
    
    #multi = service.MultiService()
    #pool = threadpool.ThreadPool()
    #tps = ThreadPoolService(pool)
    #tps.setServiceParent(multi)
    #resource = wsgi.WSGIResource(reactor, tps.pool, WSGIHandler())
    #root = twresource.Root(resource)
    
    
    #Admin Site media files
    #staticrsrc = static.File(os.path.join(os.path.abspath("."), "/usr/haridas/eclipse_workplace/skgargpms/django/contrib/admin/media/"))
    #root.putChild("admin/media", staticrsrc)
    
    # Serve it up:
    main_site = server.Site(root)
    #internet.TCPServer(PORT, main_site).setServiceParent(multi)
    internet.TCPServer(PORT, main_site).setServiceParent(application)
    
    #EOF.
    

    使用上面的代码它使用“twisted -ny server.py”从命令行运行良好,但是当我们将它作为守护程序“twisted -y server.py”运行时,它会挂起,但应用程序正在侦听端口8080我可以使用telnet访问它。

    我从stackoverflow本身找到了这个悬而未决问题的一些修复。它帮助我使用下面给出的代码部分,在上面的server.py文件中进行了注释。

    multi = service.MultiService()
    pool = threadpool.ThreadPool()
    tps = ThreadPoolService(pool)
    tps.setServiceParent(multi)
    resource = wsgi.WSGIResource(reactor, tps.pool, WSGIHandler())
    root = twresource.Root(resource)
    

    和: -

    internet.TCPServer(PORT, main_site).setServiceParent(multi)
    

    而不是使用: -

    wsgi_root = wsgi_resource()
    root = Root(wsgi_root)
    

    和: -

    internet.TCPServer(PORT, main_site).setServiceParent(application)
    

    修改后的方法也没有帮助我避免悬挂问题。在扭曲的守护进程模式下是否有成功运行django应用程序的任何机构?。

    我在编写这些代码时会出现任何错误吗?目前我只是开始详细了解扭曲的架构。请帮我解决这个问题

    谢谢和问候,

    Haridas N。

    注意: - 我正在寻找Twisted Application配置(TAC)文件,该文件将django应用程序与扭曲和运行集成,并且在守护进程模式下也没有任何问题。

    谢谢你, Haridas N.

1 个答案:

答案 0 :(得分:0)

twistd Twisted Daemonizer 。使用twistd运行的任何内容都很容易守护。您所要做的就是 not 传递--nodaemon选项。

至于为什么你的代码“不工作”,你需要提供更多关于你做了什么,你期望发生什么,以及实际发生的事情与你的期望不同的细节。否则,只有魔术师才能回答你的问题。

由于您说TCP端口甚至没有设置,我能想到的唯一猜测就是您试图在没有权限的情况下侦听特权端口(例如80)(即,你不是root用户而且你没有使用authbind或类似的东西。)

相关问题