I am new to the twisted world, so first I try to configure my working django project, configured for twisted, currently working well on the django or apache testing server via mod_wsgi.
I followed this link and this too , to configure the setting based on which I have the server.py file below.
So, to integrate the django application with twisted, I used the following code,
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
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
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()
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
wsgi_resource = wsgi.WSGIResource(reactor, pool, WSGIHandler())
return wsgi_resource
application = service.Application('twisted-django')
wsgi_root = wsgi_resource()
root = Root(wsgi_root)
main_site = server.Site(root)
internet.TCPServer(PORT, main_site).setServiceParent(application)
Using the above code It worked well from the command line using "twisted -ny server.py", but when we run it as the "twisted -y server.py" daemon, it will hang, but the application listens on port 8080. I can access him using 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 Application (TAC), django .
,
.