• 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).

    参考:

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

  • 相关阅读:
    bzoj1379 [Baltic2001]Postman
    bzoj1116 [POI2008]CLO
    bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛
    tyvj1086 Elevator
    2014.7.8模拟赛【聪明的打字员】
    2014.7.8模拟赛【笨笨当粉刷匠】|bzoj1296 [SCOI]粉刷匠
    2014.7.8模拟赛【笨笨的电话网络】
    2014.7.8模拟赛【词编码】
    bzoj1854 [Scoi2010]游戏
    2014.7.7 模拟赛【小K的农场】
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3991913.html
Copyright © 2020-2023  润新知