並行執行 (Concurrent Execution)
*******************************

本章節描述的模組在程式的並行執行上提供支援。選擇要使用哪一個工具則取決
於是執行什麼樣的任務（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"

  * Thread-Local Data

    * "local"

  * Thread Objects

    * "Thread"

      * "start()"

      * "run()"

      * "join()"

      * "name"

      * "getName()"

      * "setName()"

      * "ident"

      * "native_id"

      * "is_alive()"

      * "daemon"

      * "isDaemon()"

      * "setDaemon()"

  * Lock 物件

    * "Lock"

      * "acquire()"

      * "release()"

      * "locked()"

  * RLock 物件

    * "RLock"

      * "acquire()"

      * "release()"

  * Condition Objects

    * "Condition"

      * "acquire()"

      * "release()"

      * "wait()"

      * "wait_for()"

      * "notify()"

      * "notify_all()"

  * Semaphore Objects

    * "Semaphore"

      * "acquire()"

      * "release()"

    * "BoundedSemaphore"

    * "Semaphore" 範例

  * Event Objects

    * "Event"

      * "is_set()"

      * "set()"

      * "clear()"

      * "wait()"

  * Timer Objects

    * "Timer"

      * "cancel()"

  * Barrier Objects

    * "Barrier"

      * "wait()"

      * "reset()"

      * "abort()"

      * "parties"

      * "n_waiting"

      * "broken"

    * "BrokenBarrierError"

  * Using locks, conditions, and semaphores in the "with" statement

* "multiprocessing" --- 以行程為基礎的平行性

  * 簡介

    * "Process" 類別

    * Contexts and start methods

    * Exchanging objects between processes

    * Synchronization between processes

    * Sharing state between processes

    * Using a pool of workers

  * Reference

    * "Process" 與例外

      * "Process"

        * "run()"

        * "start()"

        * "join()"

        * "name"

        * "is_alive()"

        * "daemon"

        * "pid"

        * "exitcode"

        * "authkey"

        * "sentinel"

        * "terminate()"

        * "kill()"

        * "close()"

      * "ProcessError"

      * "BufferTooShort"

      * "AuthenticationError"

      * "TimeoutError"

    * Pipes and Queues

      * "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()"

    * Miscellaneous

      * "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 Objects

      * "Connection"

        * "send()"

        * "recv()"

        * "fileno()"

        * "close()"

        * "poll()"

        * "send_bytes()"

        * "recv_bytes()"

        * "recv_bytes_into()"

    * Synchronization primitives

      * "Barrier"

      * "BoundedSemaphore"

      * "Condition"

      * "Event"

      * "Lock"

        * "acquire()"

        * "release()"

      * "RLock"

        * "acquire()"

        * "release()"

      * "Semaphore"

    * 共享的 "ctypes" 物件

      * "Value()"

      * "Array()"

      * "multiprocessing.sharedctypes" 模組

        * "RawArray()"

        * "RawValue()"

        * "Array()"

        * "Value()"

        * "copy()"

        * "synchronized()"

    * Managers

      * "Manager()"

      * "BaseManager"

        * "start()"

        * "get_server()"

        * "connect()"

        * "shutdown()"

        * "register()"

        * "address"

      * "SyncManager"

        * "Barrier()"

        * "BoundedSemaphore()"

        * "Condition()"

        * "Event()"

        * "Lock()"

        * "Namespace()"

        * "Queue()"

        * "RLock()"

        * "Semaphore()"

        * "Array()"

        * "Value()"

        * "dict()"

        * "list()"

      * "Namespace"

      * Customized managers

      * Using a remote manager

    * Proxy Objects

      * "BaseProxy"

        * "_callmethod()"

        * "_getvalue()"

        * "__repr__()"

        * "__str__()"

      * Cleanup

    * Process Pools

      * "Pool"

        * "apply()"

        * "apply_async()"

        * "map()"

        * "map_async()"

        * "imap()"

        * "imap_unordered()"

        * "starmap()"

        * "starmap_async()"

        * "close()"

        * "terminate()"

        * "join()"

      * "AsyncResult"

        * "get()"

        * "wait()"

        * "ready()"

        * "successful()"

    * Listeners and Clients

      * "deliver_challenge()"

      * "answer_challenge()"

      * "Client()"

      * "Listener"

        * "accept()"

        * "close()"

        * "address"

        * "last_accepted"

      * "wait()"

      * Address Formats

    * Authentication keys

    * Logging

      * "get_logger()"

      * "log_to_stderr()"

    * "multiprocessing.dummy" 模組

      * "ThreadPool"

  * Programming guidelines

    * All start methods

    * The *spawn* and *forkserver* start methods

  * 範例

* "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()"

  * 例外類別

    * "CancelledError"

    * "TimeoutError"

    * "BrokenExecutor"

    * "InvalidStateError"

    * "BrokenThreadPool"

    * "BrokenInterpreterPool"

    * "ExecutionFailed"

    * "BrokenProcessPool"

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

  * Using the "subprocess" Module

    * "run()"

    * "CompletedProcess"

      * "args"

      * "returncode"

      * "stdout"

      * "stderr"

      * "check_returncode()"

    * "DEVNULL"

    * "PIPE"

    * "STDOUT"

    * "SubprocessError"

    * "TimeoutExpired"

      * "cmd"

      * "timeout"

      * "output"

      * "stdout"

      * "stderr"

    * "CalledProcessError"

      * "returncode"

      * "cmd"

      * "output"

      * "stdout"

      * "stderr"

    * Frequently Used Arguments

    * Popen Constructor

      * "Popen"

    * 例外

  * 安全性注意事項

  * Popen Objects

    * "poll()"

    * "wait()"

    * "communicate()"

    * "send_signal()"

    * "terminate()"

    * "kill()"

    * "args"

    * "stdin"

    * "stdout"

    * "stderr"

    * "pid"

    * "returncode"

  * Windows Popen Helpers

    * "STARTUPINFO"

      * "dwFlags"

      * "hStdInput"

      * "hStdOutput"

      * "hStdError"

      * "wShowWindow"

      * "lpAttributeList"

    * Windows Constants

      * "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"

  * Older high-level API

    * "call()"

    * "check_call()"

    * "check_output()"

  * Replacing Older Functions with the "subprocess" Module

    * Replacing **/bin/sh** shell command substitution

    * Replacing shell pipeline

    * Replacing "os.system()"

    * Replacing the "os.spawn" family

    * Replacing "os.popen()"

  * Legacy Shell Invocation Functions

    * "getstatusoutput()"

    * "getoutput()"

  * 註解

    * Converting an argument sequence to a string on Windows

    * Disable use of "posix_spawn()"

* "sched" --- 事件排程器

  * "scheduler"

  * 排程器物件

    * "enterabs()"

    * "enter()"

    * "cancel()"

    * "empty()"

    * "run()"

    * "queue"

* "queue" --- 同步佇列 (synchronized queue) 類別

  * "Queue"

  * "LifoQueue"

  * "PriorityQueue"

  * "SimpleQueue"

  * "Empty"

  * "Full"

  * "ShutDown"

  * 佇列物件

    * "qsize()"

    * "empty()"

    * "full()"

    * "put()"

    * "put_nowait()"

    * "get()"

    * "get_nowait()"

    * "task_done()"

    * "join()"

    * Terminating queues

      * "shutdown()"

  * SimpleQueue 物件

    * "qsize()"

    * "empty()"

    * "put()"

    * "put_nowait()"

    * "get()"

    * "get_nowait()"

* "contextvars" --- 情境變數

  * Context Variables

    * "ContextVar"

      * "name"

      * "get()"

      * "set()"

      * "reset()"

    * "Token"

      * "var"

      * "old_value"

      * "MISSING"

  * Manual Context Management

    * "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()"
