tornado : 异步、非阻塞

 

The terms asynchronous and non-blocking are closely related and are often used interchangeably, but they are not quite the

same thing.

Blocking

A function blocks when it waits for something to happen before returning. A function may block for many reasons: network I/O,

disk I/O, mutexes, etc. In fact, every function blocks, at least a little bit, while it is running and using the CPU (for an extreme

example that demonstrates why CPU blocking must be taken as seriously as other kinds of blocking, consider password hashing

functions like bcrypt, which by design use hundreds of milliseconds of CPU time, far more than a typical network or disk access).

 

A function can be blocking in some respects and non-blocking in others. For example,tornado.httpclient in the default

configuration blocks on DNS resolution but not on other network access (to mitigate this use ThreadedResolver or a 

tornado.curl_httpclient with a properly-configured build of libcurl). In the context of Tornado we generally talk about

blocking in the context of network I/O, although all kinds of blocking are to be minimized.

 

Asynchronous

An asynchronous function returns before it is finished, and generally causes some work to happen in the background before triggering

some future action in the application (as opposed to normal synchronous functions, which do everything they are going to do before

returning). There are many styles of asynchronous interfaces:

  • Callback argument
  • Return a placeholder (FuturePromiseDeferred)
  • Deliver to a queue
  • Callback registry (e.g. POSIX signals)

Regardless of which type of interface is used, asynchronous functions by definition interact differently with their callers; there is no free

way to make a synchronous function asynchronous in a way that is transparent to its callers (systems like gevent use lightweight threads

to offer performance comparable to asynchronous systems, but they do not actually make things asynchronous).

 

参考:

同步、异步、阻塞、非阻塞

posted @ 2014-09-25 01:10  等风来。。  Views(313)  Comments(0Edit  收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------