并发执行
********

本章中描述的模块支持并发执行代码。 适当的工具选择取决于要执行的任务（
CPU密集型或IO密集型）和偏好的开发风格（事件驱动的协作式多任务或抢占式
多任务处理）。 这是一个概述：

* "threading" --- 基于线程的并行

  * "active_count()"

  * "current_thread()"

  * "excepthook()"

  * "__excepthook__"

  * "get_ident()"

  * "get_native_id()"

  * "enumerate()"

  * "main_thread()"

  * "settrace()"

  * "settrace_all_threads()"

  * "gettrace()"

  * "setprofile()"

  * "setprofile_all_threads()"

  * "getprofile()"

  * "stack_size()"

  * "TIMEOUT_MAX"

  * 线程本地数据

    * "local"

  * 线程对象

    * "Thread"

      * "start()"

      * "run()"

      * "join()"

      * "name"

      * "getName()"

      * "setName()"

      * "ident"

      * "native_id"

      * "is_alive()"

      * "daemon"

      * "isDaemon()"

      * "setDaemon()"

  * 锁对象

    * "Lock"

      * "acquire()"

      * "release()"

      * "locked()"

  * 递归锁对象

    * "RLock"

      * "acquire()"

      * "release()"

  * 条件对象

    * "Condition"

      * "acquire()"

      * "release()"

      * "wait()"

      * "wait_for()"

      * "notify()"

      * "notify_all()"

  * 信号量对象

    * "Semaphore"

      * "acquire()"

      * "release()"

    * "BoundedSemaphore"

    * "Semaphore" 例子

  * 事件对象

    * "Event"

      * "is_set()"

      * "set()"

      * "clear()"

      * "wait()"

  * 定时器对象

    * "Timer"

      * "cancel()"

  * 栅栏对象

    * "Barrier"

      * "wait()"

      * "reset()"

      * "abort()"

      * "parties"

      * "n_waiting"

      * "broken"

    * "BrokenBarrierError"

  * 在 "with" 语句中使用锁、条件和信号量

* "multiprocessing" --- 基于进程的并行

  * 概述

    * "Process" 类

    * 上下文和启动方法

    * 在进程之间交换对象

    * 进程间同步

    * 进程间共享状态

    * 使用工作进程

  * 参考

    * "Process" 和异常

      * "Process"

        * "run()"

        * "start()"

        * "join()"

        * "name"

        * "is_alive()"

        * "daemon"

        * "pid"

        * "exitcode"

        * "authkey"

        * "sentinel"

        * "terminate()"

        * "kill()"

        * "close()"

      * "ProcessError"

      * "BufferTooShort"

      * "AuthenticationError"

      * "TimeoutError"

    * 管道和队列

      * "Pipe()"

      * "Queue"

        * "qsize()"

        * "empty()"

        * "full()"

        * "put()"

        * "put_nowait()"

        * "get()"

        * "get_nowait()"

        * "close()"

        * "join_thread()"

        * "cancel_join_thread()"

      * "SimpleQueue"

        * "close()"

        * "empty()"

        * "get()"

        * "put()"

      * "JoinableQueue"

        * "task_done()"

        * "join()"

    * 杂项

      * "active_children()"

      * "cpu_count()"

      * "current_process()"

      * "parent_process()"

      * "freeze_support()"

      * "get_all_start_methods()"

      * "get_context()"

      * "get_start_method()"

      * "set_executable()"

      * "set_forkserver_preload()"

      * "set_start_method()"

    * 连接对象（Connection）

      * "Connection"

        * "send()"

        * "recv()"

        * "fileno()"

        * "close()"

        * "poll()"

        * "send_bytes()"

        * "recv_bytes()"

        * "recv_bytes_into()"

    * 同步原语

      * "Barrier"

      * "BoundedSemaphore"

      * "Condition"

      * "Event"

      * "Lock"

        * "acquire()"

        * "release()"

      * "RLock"

        * "acquire()"

        * "release()"

      * "Semaphore"

    * 共享 "ctypes" 对象

      * "Value()"

      * "Array()"

      * "multiprocessing.sharedctypes" 模块

        * "RawArray()"

        * "RawValue()"

        * "Array()"

        * "Value()"

        * "copy()"

        * "synchronized()"

    * 管理器

      * "Manager()"

      * "BaseManager"

        * "start()"

        * "get_server()"

        * "connect()"

        * "shutdown()"

        * "register()"

        * "address"

      * "SyncManager"

        * "Barrier()"

        * "BoundedSemaphore()"

        * "Condition()"

        * "Event()"

        * "Lock()"

        * "Namespace()"

        * "Queue()"

        * "RLock()"

        * "Semaphore()"

        * "Array()"

        * "Value()"

        * "dict()"

        * "list()"

      * "Namespace"

      * 自定义管理器

      * 使用远程管理器

    * 代理对象

      * "BaseProxy"

        * "_callmethod()"

        * "_getvalue()"

        * "__repr__()"

        * "__str__()"

      * 清理

    * 进程池

      * "Pool"

        * "apply()"

        * "apply_async()"

        * "map()"

        * "map_async()"

        * "imap()"

        * "imap_unordered()"

        * "starmap()"

        * "starmap_async()"

        * "close()"

        * "terminate()"

        * "join()"

      * "AsyncResult"

        * "get()"

        * "wait()"

        * "ready()"

        * "successful()"

    * 监听器及客户端

      * "deliver_challenge()"

      * "answer_challenge()"

      * "Client()"

      * "Listener"

        * "accept()"

        * "close()"

        * "address"

        * "last_accepted"

      * "wait()"

      * 地址格式

    * 认证密码

    * 日志记录

      * "get_logger()"

      * "log_to_stderr()"

    * "multiprocessing.dummy" 模块

      * "ThreadPool"

  * 编程指导

    * 所有start方法

    * *spawn* 和 *forkserver* 启动方式

  * 例子

* "multiprocessing.shared_memory" --- 可跨进程直接访问的共享内存

  * "SharedMemory"

    * "close()"

    * "unlink()"

    * "buf"

    * "name"

    * "size"

  * "SharedMemoryManager"

    * "SharedMemory()"

    * "ShareableList()"

  * "ShareableList"

    * "count()"

    * "index()"

    * "format"

    * "shm"

* "concurrent" 包

* "concurrent.futures" --- 启动并行任务

  * Executor 对象

    * "Executor"

      * "submit()"

      * "map()"

      * "shutdown()"

  * ThreadPoolExecutor

    * "ThreadPoolExecutor"

    * ThreadPoolExecutor 例子

  * InterpreterPoolExecutor

    * "InterpreterPoolExecutor"

  * ProcessPoolExecutor

    * "ProcessPoolExecutor"

    * ProcessPoolExecutor 例子

  * Future 对象

    * "Future"

      * "cancel()"

      * "cancelled()"

      * "running()"

      * "done()"

      * "result()"

      * "exception()"

      * "add_done_callback()"

      * "set_running_or_notify_cancel()"

      * "set_result()"

      * "set_exception()"

  * 模块函数

    * "wait()"

      * "FIRST_COMPLETED"

      * "FIRST_EXCEPTION"

      * "ALL_COMPLETED"

    * "as_completed()"

  * Exception 类

    * "CancelledError"

    * "TimeoutError"

    * "BrokenExecutor"

    * "InvalidStateError"

    * "BrokenThreadPool"

    * "BrokenInterpreterPool"

    * "ExecutionFailed"

    * "BrokenProcessPool"

* "subprocess" --- 子进程管理

  * 使用 "subprocess" 模块

    * "run()"

    * "CompletedProcess"

      * "args"

      * "returncode"

      * "stdout"

      * "stderr"

      * "check_returncode()"

    * "DEVNULL"

    * "PIPE"

    * "STDOUT"

    * "SubprocessError"

    * "TimeoutExpired"

      * "cmd"

      * "timeout"

      * "output"

      * "stdout"

      * "stderr"

    * "CalledProcessError"

      * "returncode"

      * "cmd"

      * "output"

      * "stdout"

      * "stderr"

    * 常用参数

    * Popen 构造函数

      * "Popen"

    * 异常

  * 安全考量

  * Popen 对象

    * "poll()"

    * "wait()"

    * "communicate()"

    * "send_signal()"

    * "terminate()"

    * "kill()"

    * "args"

    * "stdin"

    * "stdout"

    * "stderr"

    * "pid"

    * "returncode"

  * Windows Popen 助手

    * "STARTUPINFO"

      * "dwFlags"

      * "hStdInput"

      * "hStdOutput"

      * "hStdError"

      * "wShowWindow"

      * "lpAttributeList"

    * Windows 常数

      * "STD_INPUT_HANDLE"

      * "STD_OUTPUT_HANDLE"

      * "STD_ERROR_HANDLE"

      * "SW_HIDE"

      * "STARTF_USESTDHANDLES"

      * "STARTF_USESHOWWINDOW"

      * "STARTF_FORCEONFEEDBACK"

      * "STARTF_FORCEOFFFEEDBACK"

      * "CREATE_NEW_CONSOLE"

      * "CREATE_NEW_PROCESS_GROUP"

      * "ABOVE_NORMAL_PRIORITY_CLASS"

      * "BELOW_NORMAL_PRIORITY_CLASS"

      * "HIGH_PRIORITY_CLASS"

      * "IDLE_PRIORITY_CLASS"

      * "NORMAL_PRIORITY_CLASS"

      * "REALTIME_PRIORITY_CLASS"

      * "CREATE_NO_WINDOW"

      * "DETACHED_PROCESS"

      * "CREATE_DEFAULT_ERROR_MODE"

      * "CREATE_BREAKAWAY_FROM_JOB"

  * 较旧的高阶 API

    * "call()"

    * "check_call()"

    * "check_output()"

  * 使用 "subprocess" 模块替换旧函数

    * 替代 **/bin/sh** shell 命令替换

    * 替代 shell 管道

    * 替代 "os.system()"

    * 替代 "os.spawn" 函数族

    * Replacing "os.popen()"

  * 旧式的 Shell 发起函数

    * "getstatusoutput()"

    * "getoutput()"

  * 备注

    * 在 Windows 上将参数列表转换为一个字符串

    * Disable use of "posix_spawn()"

* "sched" --- 事件调度器

  * "scheduler"

  * 调度器对象

    * "enterabs()"

    * "enter()"

    * "cancel()"

    * "empty()"

    * "run()"

    * "queue"

* "queue" --- 同步队列类

  * "Queue"

  * "LifoQueue"

  * "PriorityQueue"

  * "SimpleQueue"

  * "Empty"

  * "Full"

  * "ShutDown"

  * Queue对象

    * "qsize()"

    * "empty()"

    * "full()"

    * "put()"

    * "put_nowait()"

    * "get()"

    * "get_nowait()"

    * "task_done()"

    * "join()"

    * 终结队列

      * "shutdown()"

  * SimpleQueue 对象

    * "qsize()"

    * "empty()"

    * "put()"

    * "put_nowait()"

    * "get()"

    * "get_nowait()"

* "contextvars" --- 上下文变量

  * 上下文变量

    * "ContextVar"

      * "name"

      * "get()"

      * "set()"

      * "reset()"

    * "Token"

      * "var"

      * "old_value"

      * "MISSING"

  * 手动上下文管理

    * "copy_context()"

    * "Context"

      * "run()"

      * "copy()"

      * "get()"

      * "keys()"

      * "values()"

      * "items()"

  * asyncio 支持

以下是上述某些服务的支持模块：

* "_thread" --- 低层级多线程 API

  * "error"

  * "LockType"

  * "start_new_thread()"

  * "interrupt_main()"

  * "exit()"

  * "allocate_lock()"

  * "get_ident()"

  * "get_native_id()"

  * "stack_size()"

  * "TIMEOUT_MAX"

  * "acquire()"

  * "release()"

  * "locked()"
