Bpstudy34 Tornado

31
Tornado bpstudy#34 wozozo
  • date post

    13-Sep-2014
  • Category

    Technology

  • view

    2.859
  • download

    1

description

 

Transcript of Bpstudy34 Tornado

Page 1: Bpstudy34 Tornado

Tornadobpstudy#34 wozozo

Page 2: Bpstudy34 Tornado

topic

• Django

• Tornado

• Tornado with Django

Page 3: Bpstudy34 Tornado

topic

• Django

• Tornado

• Tornado with Django

Page 4: Bpstudy34 Tornado

http://www.djangoproject.com/

Page 5: Bpstudy34 Tornado

役割

• チャット以外の機能全部

Page 6: Bpstudy34 Tornado

topic

• Django

• Tornado

• Tornado with Django

Page 7: Bpstudy34 Tornado

http://www.tornadoweb.org/

Page 8: Bpstudy34 Tornado

Tornado is an open source version of the scalable, non-blocking web server and tools that power

FriendFeed.

Page 9: Bpstudy34 Tornado

なんかすごそうですよ!! bucho!!

Page 10: Bpstudy34 Tornado

なんかすごそうなサーバーde show.

Page 11: Bpstudy34 Tornado

リスペクトする bucho がそう言うなら使ってみよう

Page 12: Bpstudy34 Tornado

.....

Page 13: Bpstudy34 Tornado

nginx

Django

MySQL(master & slave)

Tornado

Django

Tornado

System

Polling Polling

Page 14: Bpstudy34 Tornado

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

Page 15: Bpstudy34 Tornado

chat sample

http://bit.ly/tornado-chat(tornado demo)

Page 16: Bpstudy34 Tornado

Clients(Flash)

Tornado

Polling Post

Page 17: Bpstudy34 Tornado

Client(Flash)

TornadoTimeout, SleepEvent

Django(Cron/m)

1. notification(http)

2. response

3. polling

Page 18: Bpstudy34 Tornado

code

def main(): http_server = tornado.httpserver.HTTPServer(Application()) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()

Page 19: Bpstudy34 Tornado

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)

Page 20: Bpstudy34 Tornado

class MainHandler(BaseHandler): @tornado.web.authenticated def get(self): self.render("index.html", messages=MessageMixin.cache)

Page 21: Bpstudy34 Tornado

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

// カーソルの中身初期化

Page 22: Bpstudy34 Tornado

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])

Page 23: Bpstudy34 Tornado

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))

Page 24: Bpstudy34 Tornado

demo

Page 25: Bpstudy34 Tornado

MessageFiltering

Page 26: Bpstudy34 Tornado

MessageLog

• Pythonのリストに保持しておき、別 Thread で1秒毎にファイルに書き出す

Page 27: Bpstudy34 Tornado

Debug

• DebugAPI

import pprint

pp = pprint.PrettyPrinter(indent=4) pp.pprint(self.avatar_cache)

Page 28: Bpstudy34 Tornado

Database

• tornado.database

• SET time_zone = "+0:00

• SQLAlchemy

Page 29: Bpstudy34 Tornado

tornado 使うなら

Page 30: Bpstudy34 Tornado

0.2 or Repository

• websocket.py

• win32_support.py

• httpclient

• AsyncHTTPClient2 class

• immune to the fd > 1024

• bugfix

Page 31: Bpstudy34 Tornado

END