简体   繁体   中英

Tornado: Make Blocking HTTP Request in Async Handler

I'm trying to write an app in Tornado that, upon receiving a POST request containing a category, will call a function that will make (blocking) HTTP requests to a list of RSS feeds (held in a CouchDB database) with that topic, then perform some sorting on the data, returning a dictionary to the Tornado async request handler. So far, it seems to work relatively well the first time the user accesses it.

On the second POST request to the same page, it will often combine results gathered from more than one category, as if the function calls for each category are being combined for some reason. I'm new to event-driven programming and I have no idea why this is happening. It's getting to be a mess, as a user will click on one category and get results for both.

Here's a bit of a diagram of the structure:

Calls:  Tornado Async Handler -> get_data(category) -> RSS_handler(RSS_feed)
Returns: self.write(data)     <- more_data          <- some_data

Any idea why these calls are getting combined? My RSS handler class clears out all member variables in its init function, as does the get_data function that does the processing.

(Would've included code, but there's too much of it to really paste here, and I have no idea which line is causing it)

without more details it's hard to answer. It sounds like an issue using global variables or something like that. A good way to help debug this type of issue is to add logging statements before and after you modify data at each step.

run your tornado app with --logging=debug on the command line to enable DEBUG level logging. Then add logging statements to help track down where the extra data is added.

import logging
...
def on_response(self, response):
    logging.debug(response)
    self.write(response.body)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM