I read the book Introduction to Tornado. It introduces the asynchronous feature of tornado in an application using twitter search API.
The code is as follows:
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
query = self.get_argument('q')
client = tornado.httpclient.AsyncHTTPClient()
response = yield tornado.gen.Task(client.fetch,
"http://search.twitter.com/search.json?" + \
urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
...
self.finish()
It uses the v1 twitter API to search a keywork. However, the new v1.1 twitter API forbids the use of non oauth request. As a result, I have to use the oauth library with my consumer key and access key to request the twitter search API.
def request_twitter(url, http_method = 'GET', post_body = '', http_headers = ''):
consumer = oauth.Consumer(key = consumer_key, secret = consumer_secret)
token = oauth.Token(key = access_token, secret = access_secret)
client = oauth.Client(consumer, token)
request = client.request(url, method = http_method, body = post_body, headers = http_headers)
return request
But the oauth client doesn't provide the asynchronous way to request. So I want to know how can I make the asynchronous request using oauth client to twitter API in python? Thanks.
Take a look at the TwitterMixin that is supplied with Tornado and study its code a bit.