【问题标题】:Twisted hangs while daemonizeTwisted 在守护进程时挂起
【发布时间】:2011-06-28 22:52:06
【问题描述】:

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

我按照 linkthis too 配置设置,基于我有一个 server.py 文件,如下所示

所以为了将 django 应用程序与 twisted 集成,我使用了以下代码,

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 应用程序?

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

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

感谢和问候,

哈里达斯 N.

【问题讨论】:

    标签: django twisted comet


    【解决方案1】:

    我想你快到了。只需在最后添加一行:

    multi.setServiceParent(application)
    

    【讨论】:

    • 是的!,唯一缺少的是那条线。现在一切正常,没有任何阻塞....谢谢。
    • 不客气。你介意正式接受我的回答吗? :)
    • StackOverflow 需要在纠缠问题海报以接受答案方面做得更好(像亚马逊和 Ebay 这样的微妙提醒会很好)。有些人总是记得接受,有些人不记得。 :-/
    猜你喜欢
    • 2018-04-25
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多