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

发布于 2024-10-17 07:04:54 字数 4316 浏览 3 评论 0原文

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

我尝试按照以下链接来实现此目的,

  1. Twisted:创建线程池然后守护进程会导致无信息挂起

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

但这也不是为我工作。通过这种方法,TCP 服务器甚至不监听给定的端口。

请帮我弄清楚。

更新

对于缺少的信息,我深表歉意。这是我的目标。

我是twisted世界的初学者,所以首先我尝试在twisted下配置我的工作django项目,目前它在django测试服务器或通过mod_wsgi的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 应用程序?

我在组合这些代码时犯了任何错误吗?目前我才开始详细学习扭曲的架构。请帮助我解决这个问题

谢谢和问候,

Haridas N。

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

谢谢你, 哈里达斯·N.

Last two day I'm trying to find a way to run a working django project under twisted. After detailed searching I found several methods to configure it. But most of them are deal with how to run the app via command line not as a daemon.I want to run the django project as daemon.

I tried following links to implement this,

  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/

But this also not working for me.By this method TCP server is not even listen to the given port.

Please help me to figure it out.

UPDATE

I'm sorry for the missing informations.Here is my objectives.

I'm beginner in twisted world, so first I'm trying to get my working django project configured under twisted,currently its working well on django testing server or apache via mod_wsgi.

To configure it with twisted I used the biding code given below, that code is a combination of two sample's found in the links that I given in the first post.

So in-order to integrate django app with twisted I used the following code, file name: "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.

Using above code It worked well from command line using "twisted -ny server.py", but when we run it as daemon "twisted -y server.py" it will hang, but the app is listening to the port 8080. I can access it using telnet.

I found some fixes for this hanging issue from stackoverflow itself. It helped me to use the code sections given below, which is commented in the above server.py file.

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

and :-

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

instead of using the:-

wsgi_root = wsgi_resource()
root = Root(wsgi_root)

and :-

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

The modified method also didn't helped me to avoid the hanging issue.Is any body out there who successfully run the django apps under twisted daemon mode?.

I maid any mistakes while combining these codes?, Currently I'm only started to learn the twisted architectures in detail. Please help me to solve this problem

Thanks and Regards,

Haridas N.

Note:- Im looking for the Twisted Application configuration (TAC) file, which integrate django app with twisted and run with out any problem in the daemon mode also.

Thank you,
Haridas N.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2024-10-24 07:04:54

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

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

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

twistd is the Twisted Daemonizer. Anything you run with twistd will be easy to daemonize. All you have to do is not pass the --nodaemon option.

As far as why your code is "not working", you need to provide more details about what you did, what you expected to happen, and how what actually happened differed from your expectations. Otherwise, only a magician can answer your question.

Since you said the TCP port doesn't even get set up, the only guess I can think of is that you're trying to listen on a privileged port (such as 80) without having permissions to do so (ie, you're not root and you're not using authbind or something similar).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文