Bottle and asyncio

1,622 views
Skip to first unread message

Don Brown

unread,
Feb 20, 2014, 2:29:29 AM2/20/14
to bott...@googlegroups.com
I took another stab at porting Bottle over to asyncio and the fork is found here:

https://github.com/mrdon/bottle

This lets bottle run fully asynchronous using the new Python standard for such things.  A couple ways this is different than the previous effort [1]:
1. Is a full on fork to get things like run/debug working nicely(ish)
2. Adds before-first-request hook to run setup code as that code will likely need to run in the event loop
3. Removes all thread locals, opting instead for explicit request/response objects to handler functions

I made that last change because thread locals don't play nicely with async code as the different requests handled by the same thread concurrently can start stepping on each other.  There is a way to make such variables task-local, but it has some limitations, so I went with a more explicit approach.  It makes the functions more noisy but not overly so IMO.

I've started to build apps on this, so I'd like to release it to Pypi.  The current name is Bottle-Async, but I'm happy to change it to something else or even something w/o bottle in it at all.

Obligatory hello world:

import asyncio
from bottle import Bottle, template

app = Bottle(__name__)
@app.route('/hello/<name>')
@asyncio.coroutine
def index(request, response, name):
    yield from asyncio.sleep(1)
    return template('<b>Hello {{name}}</b>!', name=name)

app.run(host='localhost', port=8080)

Don

[1] https://groups.google.com/forum/#!topic/bottlepy/saZdaFWZMps

Sasha Kacanski

unread,
Jan 17, 2015, 8:38:39 PM1/17/15
to bott...@googlegroups.com, donald...@gmail.com
This is cool!
in this implementation the rest serer is also covered...?

Johan Hartzenberg

unread,
Jan 19, 2015, 3:56:17 AM1/19/15
to bott...@googlegroups.com, donald...@gmail.com
Awesome!
Reply all
Reply to author
Forward
0 new messages