问题描述
Tornado宣传自己为一个相对简单的无阻塞网络服务器框架",旨在解决C10k问题.但是,在查看他们的包装MySQLdb的数据库包装器时,我遇到了以下代码:
Tornado advertises itself as "a relatively simple, non-blocking web server framework" and was designed to solve the C10k problem. However, looking at their database wrapper, which wraps MySQLdb, I came across the following piece of code:
def _execute(self, cursor, query, parameters):
try:
return cursor.execute(query, parameters)
except OperationalError:
logging.error("Error connecting to MySQL on %s", self.host)
self.close()
raise
据我所知,对建立在libmysqlclient
之上的MySQLdb的调用正在阻塞.
As far as I know calls to the MySQLdb, which is built on top of libmysqlclient
, are blocking.
我是否正确地认为长时间运行的查询会使整个Tornado服务器无响应,直到它完成或代码中是否存在魔术?
Am I right in thinking that a long-running query would render the entire Tornado server unresponsive until it finishes or is there magic on the code?
推荐答案
是的,如果没有其他措施,服务器将等待查询完成执行.这并不意味着Tornado并非非阻塞Web服务器.
Yes, absent other measures, the server will wait for the query to finish executing. That does not mean Tornado is not a non-blocking web server.
非阻塞式Web服务器"不会在网络I/O上阻塞(如果它提供静态文件服务,则可能对磁盘I/O有所规定).这并不意味着您会在应用程序中立即获得违反因果关系的指令执行.
A "non-blocking web server" doesn't block on network I/O (and may have some provision for disk I/O if it does static file serving). That does not mean you get instant, causality-violating instruction execution in your application.
进行数据库调用需要时间,就像读取文件,格式化字符串,处理模板等一样需要时间.在与服务器的主事件循环相同的线程中执行任何这些操作将阻止循环继续进行,直到完成为止.
Doing a database call takes time, just like reading files, formatting strings, processing templates, etc. takes time. Doing any of these things in the same thread as the server's main event loop will prevent the loop from moving on until you're done.
这篇关于龙卷风真的是畅通无阻吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!