Execução Concorrente
********************

Os módulos descritos neste capítulo fornecem suporte a execução
simultânea de código. A escolha apropriada da ferramenta dependerá da
tarefa a ser executada (CPU bound ou IO bound) e do estilo de
desenvolvimento preferencial (multitarefa cooperativa orientada a
eventos versus multitarefa preemptiva). Eis uma visão geral:

* "threading" --- Thread-based parallelism

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

    * "Lock"

      * "acquire()"

      * "release()"

      * "locked()"

  * Objetos RLock

    * "RLock"

      * "acquire()"

      * "release()"

  * Condition Objects

    * "Condition"

      * "acquire()"

      * "release()"

      * "wait()"

      * "wait_for()"

      * "notify()"

      * "notify_all()"

  * Semaphore Objects

    * "Semaphore"

      * "acquire()"

      * "release()"

    * "BoundedSemaphore"

    * Exemplo "Semaphore"

  * Event Objects

    * "Event"

      * "is_set()"

      * "set()"

      * "clear()"

      * "wait()"

  * Objetos Timer

    * "Timer"

      * "cancel()"

  * Barrier Objects

    * "Barrier"

      * "wait()"

      * "reset()"

      * "abort()"

      * "parties"

      * "n_waiting"

      * "broken"

    * "BrokenBarrierError"

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

* "multiprocessing" --- Paralelismo baseado em processo

  * Introdução

    * A classe "Process"

    * Contextos e métodos de inicialização

    * Trocando objetos entre processos

    * Sincronização entre processos

    * Compartilhando estado entre processos

    * Using a pool of workers

  * Referência

    * "Process" and exceptions

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

    * Diversos

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

    * Shared "ctypes" Objects

      * "Value()"

      * "Array()"

      * The "multiprocessing.sharedctypes" module

        * "RawArray()"

        * "RawValue()"

        * "Array()"

        * "Value()"

        * "copy()"

        * "synchronized()"

    * Gerenciadores

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

      * Formatos de Endereços

    * Authentication keys

    * Gerando logs

      * "get_logger()"

      * "log_to_stderr()"

    * The "multiprocessing.dummy" module

      * "ThreadPool"

  * Programming guidelines

    * All start methods

    * The *spawn* and *forkserver* start methods

  * Exemplos

* "multiprocessing.shared_memory" --- Shared memory for direct access
  across processes

  * "SharedMemory"

    * "close()"

    * "unlink()"

    * "buf"

    * "name"

    * "size"

  * "SharedMemoryManager"

    * "SharedMemory()"

    * "ShareableList()"

  * "ShareableList"

    * "count()"

    * "index()"

    * "format"

    * "shm"

* O pacote "concurrent"

* "concurrent.futures" --- Launching parallel tasks

  * Executor Objects

    * "Executor"

      * "submit()"

      * "map()"

      * "shutdown()"

  * ThreadPoolExecutor

    * "ThreadPoolExecutor"

    * Exemplo de ThreadPoolExecutor

  * "`ProcessPoolExecutor`"

    * "ProcessPoolExecutor"

    * ProcessPoolExecutor Example

  * Future Objects

    * "Future"

      * "cancel()"

      * "cancelled()"

      * "running()"

      * "done()"

      * "result()"

      * "exception()"

      * "add_done_callback()"

      * "set_running_or_notify_cancel()"

      * "set_result()"

      * "set_exception()"

  * Module Functions

    * "wait()"

      * "FIRST_COMPLETED"

      * "FIRST_EXCEPTION"

      * "ALL_COMPLETED"

    * "as_completed()"

  * Exception classes

    * "CancelledError"

    * "TimeoutError"

    * "BrokenExecutor"

    * "InvalidStateError"

    * "BrokenThreadPool"

    * "BrokenProcessPool"

* "subprocess" --- Subprocess management

  * Usando o módulo "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"

    * Argumentos usados frequentemente

    * Construtor Popen

      * "Popen"

    * Exceções

  * Considerações de Segurança

  * Objetos Popen

    * "poll()"

    * "wait()"

    * "communicate()"

    * "send_signal()"

    * "terminate()"

    * "kill()"

    * "args"

    * "stdin"

    * "stdout"

    * "stderr"

    * "pid"

    * "returncode"

  * Windows Popen Helpers

    * "STARTUPINFO"

      * "dwFlags"

      * "hStdInput"

      * "hStdOutput"

      * "hStdError"

      * "wShowWindow"

      * "lpAttributeList"

    * Constantes do 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 de alto nível mais antiga

    * "call()"

    * "check_call()"

    * "check_output()"

  * Replacing Older Functions with the "subprocess" Module

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

    * Replacing shell pipeline

    * Substituindo "os.system()"

    * Replacing the "os.spawn" family

    * Replacing "os.popen()", "os.popen2()", "os.popen3()"

    * Replacing functions from the "popen2" module

  * Legacy Shell Invocation Functions

    * "getstatusoutput()"

    * "getoutput()"

  * Notas

    * Converter uma sequência de argumentos em uma string no Windows

    * Disabling use of "vfork()" or "posix_spawn()"

* "sched" --- Event scheduler

  * "scheduler"

  * Objetos Scheduler

    * "enterabs()"

    * "enter()"

    * "cancel()"

    * "empty()"

    * "run()"

    * "queue"

* "queue" --- A synchronized queue class

  * "Queue"

  * "LifoQueue"

  * "PriorityQueue"

  * "SimpleQueue"

  * "Empty"

  * "Full"

  * "ShutDown"

  * Objetos Queue

    * "qsize()"

    * "empty()"

    * "full()"

    * "put()"

    * "put_nowait()"

    * "get()"

    * "get_nowait()"

    * "task_done()"

    * "join()"

    * Terminating queues

      * "shutdown()"

  * Objetos SimpleQueue

    * "qsize()"

    * "empty()"

    * "put()"

    * "put_nowait()"

    * "get()"

    * "get_nowait()"

* "contextvars" --- Variáveis de contexto

  * Variáveis de contexto

    * "ContextVar"

      * "name"

      * "get()"

      * "set()"

      * "reset()"

    * "Token"

      * "var"

      * "old_value"

      * "MISSING"

  * Gerenciamento de contexto manual

    * "copy_context()"

    * "Context"

      * "run()"

      * "copy()"

      * "get()"

      * "keys()"

      * "values()"

      * "items()"

  * Suporte a asyncio

A seguir, os módulos de suporte para alguns dos serviços acima:

* "_thread"--- API de segmentação de baixo nível

  * "error"

  * "LockType"

  * "start_new_thread()"

  * "interrupt_main()"

  * "exit()"

  * "allocate_lock()"

  * "get_ident()"

  * "get_native_id()"

  * "stack_size()"

  * "TIMEOUT_MAX"

  * "acquire()"

  * "release()"

  * "locked()"
