Note that there are some explanatory texts on larger screens.

plurals
  1. POasynchronous call with Tornado and pyodbc
    primarykey
    data
    text
    <p>I want to implement a web service based on tornado which can provide the database query service for users. I used the pyodbc module to connect to database and do the query. In practice, I found that printing the query result would take a long time. That is to say, if I used the following code to print the query result</p> <pre><code>while 1: data = cursor.fetchone() if not data: break self.write(data + '\n') self.flush() </code></pre> <p>and the sql command is something like</p> <pre><code>select * from &lt;a large dummy table&gt; </code></pre> <p>tornado would not print the query result until the loop is over. And it takes a long time.</p> <p>I wanna make use of tornado's non-blocking asynchronous feature to make other users also be able to use the web service even though the loop for printing the current user's query request is not finished yet.</p> <p>So I write something like:</p> <pre><code>@tornado.web.asynchronous def get(self): try: cnxn = pyodbc.connect(self.server, self.driver, self.table, self.uid, self.pwd) except Exception, e: print e return try: self.cur = cnxn.execute(self.sql) except Exception, e: print e return self.wait_for_query(callback=self.async_callback(self.on_finish)) def wait_for_query(self, callback): while 1: data = self.cur.fetchone() if not data: break self.write(data) self.flush() callback() def on_finish(self): self.finish() </code></pre> <p>I read this post: <a href="https://stackoverflow.com/questions/2317501/asynchronous-comet-query-with-tornado-and-prototype">Asynchronous COMET query with Tornado and Prototype</a> and knew my solution wouldn't work. But I certainly cannot use add_timeout, 'cause there is no way for me to figure out how long an iteration would last. So how can I work through this to achieve my goal? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload