English 中文(简体)
守护进程时扭曲挂起
原标题:Twisted hangs while daemonize

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

我关注了这个链接这也是来配置设置,基于此,我有一个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.pi”运行时,它将挂起,但应用程序正在侦听端口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。

问题回答

我想你差不多到了。只需在最后再加一行:

multi.setServiceParent(application)




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签