Transcript
Tornadobpstudy#34 wozozo
topic
• Django
• Tornado
• Tornado with Django
topic
• Django
• Tornado
• Tornado with Django
役割
• チャット以外の機能全部
topic
• Django
• Tornado
• Tornado with Django
Tornado is an open source version of the scalable, non-blocking web server and tools that power
FriendFeed.
なんかすごそうですよ!! bucho!!
なんかすごそうなサーバーde show.
リスペクトする bucho がそう言うなら使ってみよう
.....
nginx
Django
MySQL(master & slave)
Tornado
Django
Tornado
System
Polling Polling
Tornado Performance Test
• 10,000 polling
• Server (Xen)
• 1core, 512MB -> 4core, 2GB
• nginx x 1 (LoadBalancer)
• Tornado x 1 (ChatServer)
• Client (Xen)
• 2000Connections x 5
chat sample
http://bit.ly/tornado-chat(tornado demo)
Clients(Flash)
Tornado
Polling Post
Client(Flash)
TornadoTimeout, SleepEvent
Django(Cron/m)
1. notification(http)
2. response
3. polling
code
def main(): http_server = tornado.httpserver.HTTPServer(Application()) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", MainHandler), (r"/auth/login", AuthLoginHandler), (r"/a/message/new", MessageNewHandler), (r"/a/message/updates", MessageUpdatesHandler), ] settings = dict( login_url="/auth/login", template_path= hogepiyo, xsrf_cookies=True, ) tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(BaseHandler): @tornado.web.authenticated def get(self): self.render("index.html", messages=MessageMixin.cache)
class MessageMixin(object): waiters = []; cache = []; cache_size = 200
def wait_for_messages(self, callback, cursor=None): cls = MessageMixin if cursor: recent = cls.cache[index + 1:] // カーソル処理 if recent: callback(recent) return cls.waiters.append(callback)
def new_messages(self, messages): cls = MessageMixin for callback in cls.waiters: callback(messages) // message 引数に callback -> response
// カーソルの中身初期化
class MessageNewHandler(BaseHandler, MessageMixin): def post(self): message = { "id": str(uuid.uuid4()), "from": self.current_user["first_name"], "body": self.get_argument("body"), } message["html"] = self.render_string("hoge.html", message=message) if self.get_argument("next", None): self.redirect(self.get_argument("next")) else: self.write(message) self.new_messages([message])
class MessageUpdatesHandler(BaseHandler, MessageMixin): @tornado.web.asynchronous def post(self): cursor = self.get_argument("cursor", None) self.wait_for_messages(self.async_callback(self.on_new_messages), cursor=cursor)
def on_new_messages(self, messages): # Closed client connection if self.request.connection.stream.closed(): return self.finish(dict(messages=messages))
demo
MessageFiltering
MessageLog
• Pythonのリストに保持しておき、別 Thread で1秒毎にファイルに書き出す
Debug
• DebugAPI
import pprint
pp = pprint.PrettyPrinter(indent=4) pp.pprint(self.avatar_cache)
Database
• tornado.database
• SET time_zone = "+0:00
• SQLAlchemy
tornado 使うなら
0.2 or Repository
• websocket.py
• win32_support.py
• httpclient
• AsyncHTTPClient2 class
• immune to the fd > 1024
• bugfix
END
top related