【问题标题】:django project with twisted and run as "Daemon"django 项目扭曲并作为“守护进程”运行
【发布时间】:2011-06-27 18:43:04
【问题描述】:

前两天,我试图找到一种方法来在扭曲的情况下运行一个工作 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/

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

请帮我弄清楚。

更新

很抱歉缺少信息。这是我的目标。

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

要使用扭曲配置它,我使用了下面给出的投标代码,该代码是我在第一篇文章中给出的链接中找到的两个示例的组合。

所以为了将 django 应用程序与 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 应用程序?

我在组合这些代码时有什么错误吗?目前我才刚刚开始详细学习扭曲的架构。请帮我解决这个问题

感谢和问候,

哈里达斯 N.

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

谢谢你, 哈里达斯·N.

【问题讨论】:

    标签: django twisted daemon


    【解决方案1】:

    twistdTwisted Daemonizer。使用twistd 运行的任何东西都将很容易被守护。您所要做的就是传递--nodaemon 选项。

    至于您的代码“不工作”的原因,您需要提供更多详细信息,说明您做了什么、预期会发生什么以及实际发生的事情与您的预期有何不同。否则,只有魔术师才能回答您的问题。

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

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 2023-03-02
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 2012-12-03
      • 2011-08-25
      • 1970-01-01
      相关资源
      最近更新 更多