Python, async web frameworks, and MongoDB
Upcoming SlideShare
Loading in...5
×
 

Python, async web frameworks, and MongoDB

on

  • 12,544 views

A talk covering the state of the art for writing asynchronous web applications using Python and MongoDB.

A talk covering the state of the art for writing asynchronous web applications using Python and MongoDB.

Statistics

Views

Total Views
12,544
Views on SlideShare
10,385
Embed Views
2,159

Actions

Likes
17
Downloads
134
Comments
0

20 Embeds 2,159

http://emptysquare.net 1797
http://join5works.com 222
http://emptysqua.re 50
http://5works.co 26
http://www.emptysquare.net 19
http://feeds.feedburner.com 8
http://184.106.200.150 6
http://localhost 5
http://www.plurk.com 4
http://ec2-54-251-15-146.ap-southeast-1.compute.amazonaws.com 4
http://www.join5works.com 3
http://coderwall.com 3
http://ec2-122-248-224-251.ap-southeast-1.compute.amazonaws.com 3
http://iseedata.com 2
http://184-106-200-150.static.cloud-ips.com 2
http://www.iseedata.com 1
http://ec2-122-248-215-8.ap-southeast-1.compute.amazonaws.com 1
http://webcache.googleusercontent.com 1
http://translate.googleusercontent.com 1
http://www.emptysqua.re 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Python, async web frameworks, and MongoDB Python, async web frameworks, and MongoDB Presentation Transcript

  • Python, MongoDB, andasynchronous web frameworks A. Jesse Jiryu Davis jesse@10gen.com emptysquare.net
  • Agenda• Talk about web services in a really dumb (“abstract”?) way• Explain when we need async web servers• Why is async hard?• What is Tornado and how does it work?• Why am I writing a new PyMongo wrapper to work with Tornado?• How does my wrapper work?
  • CPU-bound web service Client Server socket• No need for async• Just spawn one process per core
  • Normal web service Backend Client Server (DB, web service, socket socket SAN, …)• Assume backend is unbounded• Service is bound by: • Context-switching overhead • Memory!
  • What’s async for?• Minimize resources per connection• I.e., wait for backend as cheaply as possible
  • CPU- vs. Memory-boundCrypto Most web services? Chat •CPU-bound Memory-bound
  • HTTP long-polling (“COMET”)• E.g., chat server• Async’s killer app• Short-polling is CPU-bound: tradeoff between latency and load• Long-polling is memory bound• “C10K problem”: kegel.com/c10k.html• Tornado was invented for this
  • Why is async hard to code?Client Server Backend request requesttime store state response response
  • Ways to store state this slide is in beta MultithreadingMemory per connection Greenlets / Gevent Tornado, Node.js Coding difficulty
  • What’s a greenlet?• A.K.A. “green threads”• A feature of Stackless Python, packaged as a module for standard Python• Greenlet stacks are stored on heap, copied to / from OS stack on resume / pause• Cooperative• Memory-efficient
  • Threads: State stored on OS stacks# pseudo-Pythonsock = listen()request = parse_http(sock.recv())mongo_data = db.collection.find()response = format_response(mongo_data)sock.sendall(response)
  • Gevent: State stored on greenlet stacks# pseudo-Pythonimport gevent.monkey; monkey.patch_all()sock = listen()request = parse_http(sock.recv())mongo_data = db.collection.find()response = format_response(mongo_data)sock.sendall(response)
  • Tornado: State stored in RequestHandlerclass MainHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): AsyncHTTPClient().fetch( "http://example.com", callback=self.on_response) def on_response(self, response): formatted = format_response(response) self.write(formatted) self.finish()
  • Tornado IOStreamclass IOStream(object): def read_bytes(self, num_bytes, callback): self.read_bytes = num_bytes self.read_callback = callback io_loop.add_handler( self.socket.fileno(), self.handle_events, events=READ) def handle_events(self, fd, events): data = self.socket.recv(self.read_bytes) self.read_callback(data)
  • Tornado IOLoopclass IOLoop(object): def add_handler(self, fd, handler, events): self._handlers[fd] = handler # _impl is epoll or kqueue or ... self._impl.register(fd, events) def start(self): while True: event_pairs = self._impl.poll() for fd, events in event_pairs: self._handlers[fd](fd, events)
  • Python, MongoDB, & concurrency• Threads work great with pymongo• Gevent works great with pymongo – monkey.patch_socket(); monkey.patch_thread()• Tornado works so-so – asyncmongo • No replica sets, only first batch, no SON manipulators, no document classes, … – pymongo • OK if all your queries are fast • Use extra Tornado processes
  • Introducing: “Motor”• Mongo + Tornado• Experimental• Might be official in a few months• Uses Tornado IOLoop and IOStream• Presents standard Tornado callback API• Stores state internally with greenlets• github.com/ajdavis/mongo-python-driver/tree/tornado_async
  • Motorclass MainHandler(tornado.web.RequestHandler): def __init__(self): self.c = MotorConnection() @tornado.web.asynchronous def post(self): # No-op if already open self.c.open(callback=self.connected) def connected(self, c, error): self.c.collection.insert( {‘x’:1}, callback=self.inserted) def inserted(self, result, error): self.write(’OK’) self.finish()
  • Motor internals stack depth Client IOLoop RequestHandler greenlet pymongo request start switch() IOStream.sendall(callback) returntime callback() switch() parse Mongo response schedule callback callback() HTTP response
  • Motor internals: wrapperclass MotorCollection(object): def insert(self, *args, **kwargs): callback = kwargs[callback] 1 del kwargs[callback] kwargs[safe] = True def call_insert(): # Runs on child greenlet result, error = None, None try: sync_insert = self.sync_collection.insert 3 result = sync_insert(*args, **kwargs) except Exception, e: error = e # Schedule the callback to be run on the main greenlet tornado.ioloop.IOLoop.instance().add_callback( lambda: callback(result, error) 8 ) # Start child greenlet 2 greenlet.greenlet(call_insert).switch() 6 return
  • Motor internals: fake socketclass MotorSocket(object): def __init__(self, socket): # Makes socket non-blocking self.stream = tornado.iostream.IOStream(socket) def sendall(self, data): child_gr = greenlet.getcurrent() # This is run by IOLoop on the main greenlet # when data has been sent; # switch back to child to continue processing def sendall_callback(): child_gr.switch() 7 self.stream.write(data, callback=sendall_callback) 4 # Resume main greenlet child_gr.parent.switch() 5
  • Motor• Shows a general method for asynchronizing synchronous network APIs in Python• Who wants to try it with MySQL? Thrift?• (Bonus round: resynchronizing Motor for testing)
  • Questions? A. Jesse Jiryu Davis jesse@10gen.com emptysquare.net(10gen is hiring, of course: 10gen.com/careers)