{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/",
  "atlas": "https://www.pystone.net/?node=sync-async-blocking-nonblocking#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/sync-async-blocking-nonblocking.md",
  "context": "https://www.pystone.net/notes/sync-async-blocking-nonblocking.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:sync-async-blocking-nonblocking",
  "slug": "sync-async-blocking-nonblocking",
  "title": "同步和异步&阻塞和非阻塞",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "同步和异步&阻塞和非阻塞",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "2a78b76c9444702b428d5ef4d485222852e2efa8e1c742eacfd1326451ab6b41",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "同步和异步&阻塞和非阻塞",
      "anchor": "同步和异步阻塞和非阻塞",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E5%90%8C%E6%AD%A5%E5%92%8C%E5%BC%82%E6%AD%A5%E9%98%BB%E5%A1%9E%E5%92%8C%E9%9D%9E%E9%98%BB%E5%A1%9E"
    },
    {
      "depth": 2,
      "text": "同步和异步",
      "anchor": "同步和异步",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E5%90%8C%E6%AD%A5%E5%92%8C%E5%BC%82%E6%AD%A5"
    },
    {
      "depth": 3,
      "text": "同步（Synchronous）",
      "anchor": "同步synchronous",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E5%90%8C%E6%AD%A5synchronous"
    },
    {
      "depth": 3,
      "text": "异步（Asynchronous）",
      "anchor": "异步asynchronous",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E5%BC%82%E6%AD%A5asynchronous"
    },
    {
      "depth": 3,
      "text": "如何选择",
      "anchor": "如何选择",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9"
    },
    {
      "depth": 2,
      "text": "阻塞和非阻塞",
      "anchor": "阻塞和非阻塞",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E9%98%BB%E5%A1%9E%E5%92%8C%E9%9D%9E%E9%98%BB%E5%A1%9E"
    },
    {
      "depth": 3,
      "text": "阻塞",
      "anchor": "阻塞",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E9%98%BB%E5%A1%9E"
    },
    {
      "depth": 3,
      "text": "非阻塞",
      "anchor": "非阻塞",
      "citation": "https://www.pystone.net/notes/sync-async-blocking-nonblocking/#%E9%9D%9E%E9%98%BB%E5%A1%9E"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:systems-and-network-security",
      "title": "系统基础与网络安全",
      "url": "https://www.pystone.net/notes/systems-and-network-security/",
      "atlas": "https://www.pystone.net/?node=systems-and-network-security#knowledge-atlas",
      "label": "系统基础与网络安全",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "计算机与操作系统中的“同步和异步&阻塞和非阻塞”导航项",
      "citation": "https://www.pystone.net/notes/systems-and-network-security/#%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%B8%8E%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F"
    }
  ],
  "contentMarkdown": "# 同步和异步&阻塞和非阻塞\n\n﻿# 同步和异步&阻塞和非阻塞\n\n> 创建时间：2020/6/17 21:05\n\n## 同步和异步\n\n同步和异步关注的是消息通信机制 (synchronous communication/ asynchronous communication)\n\n### 同步（Synchronous）\n\n同步，就是在发出一个 _调用_ 时，在没有得到结果之前，该 _调用_ 就不返回。但是一旦调用返回，就得到返回值了。换句话说，就是由 _调用者_ 主动等待这个 _调用_ 的结果。\n\n> 同步是指：当程序1调用程序2时，程序1停下不动，直到程序2完成回到程序1来，程序1才继续执行下去。\n\n> 同步是指：发送方发出数据后，等接收方发回响应以后才发下一个数据包的通讯方式。\n\n### 异步（Asynchronous）\n\n_调用_ 在发出之后，方法调用就会立即返回，调用者就可以继续后续的操作。换句话说，当一个异步过程调用发出后，调用者不会立刻得到结果。而是在 _调用_ 发出后， _被调用者_ 通过状态、通知来通知调用者，或通过回调函数处理这个调用。\n\n> 异步是指：当程序1调用程序2时，程序1径自继续自己的下一个动作，不受程序2的的影响。\n\n> 异步是指：发送方发出数据后，不等接收方发回响应，接着发送下个数据包的通讯方式。\n\n### 如何选择\n\n就是看需要的请求的数据是否是程序继续执行必须依赖的数据\n优缺点：\n\n  1. 同步的执行效率会比较低，耗费时间，但有利于我们对流程进行控制，避免很多不可掌控的意外情况；\n  2. 异步的执行效率高，节省时间，但是会占用更多的资源，也不利于我们对进程进行控制\n\n## 阻塞和非阻塞\n\n阻塞和非阻塞关注的是程序在等待调用结果（消息，返回值）时的状态.\n\n### 阻塞\n\n阻塞调用是指调用结果返回之前，当前线程会被挂起。调用线程只有在得到结果之后才会返回。\n\n### 非阻塞\n\n非阻塞调用指在不能立刻得到结果之前，该调用不会阻塞当前线程。\n"
}
