如何创建异步Tornado HTTP服务器?

2024-06-15 06:48:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了下面的HTTP重定向服务器。在测试期间,我发现它以阻塞方式进行调用,主要是因为对couchbase的同步调用。如何在给定的套接字上运行并行请求响应?我应该请求asyncronous调用db吗?在

class ABC(web.RequestHandler):

        def get(self):
                self.MartiniTagKey = self.get_argument("mtag", strip=True) #TEST1
                self.couchbase_start_time = time.time()
                couchbase_query_result = bucket.get(self.MartiniTagKey) #TEST2
                self.CouchbaseMS = time.time() - self.couchbase_start_time
                url_to_redirect = json.loads(couchbase_query_result[2])['metaTag'] #TEST3
                self.redirect(url_to_redirect)  



if __name__=="__main__":
        options.options.log_file_prefix = log_directory
        options.parse_command_line()
        db=MySQLdb.connect(host=mysql_host, user=mysql_user,
        passwd=mysql_password, port=int(mysql_port), db=mysql_db)
        cursor = db.cursor()
        couchbase = Couchbase(couchbase_host+':'+couchbase_port, couchbase_bucket, couchbase_password)
        process.fork_processes(0)
        bucket = couchbase[couchbase_bucket]
        app = web.Application(handlers=[(r"/", ABC),])
        app.listen(8080,"")
        io_loop = ioloop.IOLoop.instance()
        io_loop.start()

Tags: selfwebhostdbgetbuckettimeport