Created
March 14, 2014 18:46
-
-
Save tatiana/9554170 to your computer and use it in GitHub Desktop.
Sample Tornado server, both asynchronous and synchronous (blocking)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from tornado import gen, httpclient | |
from tornado.ioloop import IOLoop | |
from tornado.httpserver import HTTPServer | |
from tornado.log import app_log | |
from tornado.options import define, options, parse_command_line | |
from tornado.web import asynchronous, Application, RequestHandler, URLSpec | |
DEBUG = True | |
PORT = 8888 | |
FACEBOOK_URL = "http://api.facebook.com/restserver.php?format=json&method=links.getStats&urls={0}" | |
external_api_url = FACEBOOK_URL.format("http://globo.com") | |
# Options | |
define("debug", default=DEBUG, help="Enable or disable debug", type=bool) | |
define("port", default=PORT, help="Run app on the given port", type=int) | |
def create_app(): | |
""" | |
Create instance of tornado.web.Application. | |
""" | |
routes = [ | |
URLSpec(r'/async', MainHandlerAsync), | |
URLSpec(r"/block", MainHandlerBlocking) | |
] | |
return Application(routes, **options.as_dict()) | |
class MainHandlerBlocking(RequestHandler): | |
def get(self): | |
req = httpclient.HTTPRequest(external_api_url, method='GET') | |
# we could use something like requests or urllib here | |
client = httpclient.HTTPClient() | |
response = client.fetch(req) | |
# do something with the response (response.body) | |
self.finish("from block") | |
class MainHandlerAsync(RequestHandler): | |
@asynchronous | |
@gen.engine | |
def get(self): | |
req = httpclient.HTTPRequest(external_api_url, method='GET') | |
client = httpclient.AsyncHTTPClient() | |
# don't let the yield call confuse you, it's just Tornado helpers to make | |
# writing async code a bit easier. This is the same as doing | |
# client.fetch(req, callback=_some_other_helper_function) | |
response = yield gen.Task(client.fetch, req) | |
### do something with the response (response.body) | |
self.finish("from asynchronous") | |
def main(): | |
""" | |
Run main loop. | |
""" | |
parse_command_line() | |
application = create_app() | |
server = HTTPServer(application) | |
server.listen(options['port']) | |
app_log.info("Service is running at port {0}".format(options['port'])) | |
io_loop = IOLoop.instance() | |
io_loop.start() | |
if __name__ == '__main__': | |
main() | |
else: | |
application = create_app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment