2011-02-15 111 views
1

過去兩天,我試圖找到一種方法來運行扭曲下的工作django項目。詳細搜索後,我發現了幾種方法來配置它。但其中大部分都是關於如何通過命令行運行應用程序而不是守護進程。我想將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/

但是,這也不能工作me.By這種方法TCP服務器連聽都不聽到給定的港口。

請幫我弄明白。

UPDATE

我對遺漏的信息抱歉。這是我的目標。

我在扭曲的世界初學者,所以第一次我試圖讓下扭曲配置我的工作Django項目,目前它的Django的測試服務器或Apache通過的mod_wsgi上運作良好。

用扭曲來配置我使用了下面給出的綁定代碼,該代碼是我在第一篇文章中給出的鏈接中找到的兩個樣本的組合。

因此,爲了將django應用程序與扭曲集成,我使用了以下代碼,文件名:「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. 

使用上面的代碼使用「扭曲-ny server.py」行之有效的命令行,但是當我們運行它作爲守護「扭曲-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) 

改進的方法也沒有幫我避免懸而未決的問題。誰是成功的人在twisted守護進程模式下運行django應用程序?

我在組合這些代碼時會出現任何錯誤嗎?,目前我只是開始詳細學習扭曲的體系結構。請幫我解決這個問題

感謝和問候,

Haridas N.

注意: - 我正在尋找扭曲應用程序配置(TAC)文件,該文件將django應用程序與扭曲相結合,並在守護程序模式下運行並解決任何問題。

謝謝 Haridas N.

回答

0

twistd來扭曲Daemonizer。你用twistd運行的任何東西都很容易守護。所有你需要做的是不是通過--nodaemon選項。

至於爲什麼你的代碼是「不工作」,你需要提供你做了什麼,你希望發生什麼,以及如何實際發生的事情與你的期望不同的更多細節。否則,只有魔術師才能回答你的問題。

既然你說的TCP端口甚至沒有設置,我能想到的唯一的猜測是你試圖在特權端口(如80),而沒有權限這樣做(即,你不是根,你不使用authbind或類似的東西)。