This repository has been archived on 2023-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
django-pyvows/django_pyvows/server.py

60 lines
1.4 KiB
Python
Raw Normal View History

2011-08-19 14:57:24 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# django-pyvows extensions
# https://github.com/rafaelcaricio/django-pyvows
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
2012-03-09 17:02:27 +00:00
from threading import Thread, current_thread, local
from time import sleep
2011-08-19 14:57:24 +00:00
2012-03-09 17:02:27 +00:00
from cherrypy import wsgiserver
2011-08-19 14:57:24 +00:00
from django.core.handlers.wsgi import WSGIHandler
2012-03-09 17:02:27 +00:00
def run_app(host, port):
server = wsgiserver.CherryPyWSGIServer(
(host, port),
WSGIHandler(),
server_name='tornado-pyvows',
numthreads = 1
)
my_thread = current_thread()
my_thread.server = server
try:
server.start()
except KeyboardInterrupt:
server.stop()
class DjangoServer(object):
2011-08-19 14:57:24 +00:00
def __init__(self, host, port):
2012-03-09 17:02:27 +00:00
self.host = host
self.port = port
2011-08-19 14:57:24 +00:00
2011-08-22 21:51:39 +00:00
def start(self, settings):
2012-03-09 17:02:27 +00:00
self.thr = Thread(target=run_app, args=(self.host, self.port))
2011-08-19 14:57:24 +00:00
self.thr.daemon = True
2012-03-13 00:56:57 +00:00
self.thr.settings = {}
for k, v in settings.iteritems():
self.thr.settings[k] = v
2011-08-19 14:57:24 +00:00
self.thr.start()
2012-03-09 17:02:27 +00:00
while not len(self.thr.server.requests._threads):
sleep(0.1)
for _thread in self.thr.server.requests._threads:
2012-03-13 00:56:57 +00:00
_thread.settings = {}
2012-03-09 17:02:27 +00:00
for k, v in settings.iteritems():
2012-03-09 21:55:05 +00:00
_thread.settings[k] = v
2012-03-09 17:02:27 +00:00