万本电子书0元读

万本电子书0元读

顶部广告

Learning Concurrency in Python电子书

售       价:¥

5人正在读 | 0人评论 9.8

作       者:Elliot Forbes

出  版  社:Packt Publishing

出版时间:2017-08-16

字       数:43.0万

所属分类: 进口书 > 外文原版书 > 电脑/网络

温馨提示:数字商品不支持退换货,不提供源文件,不支持导出打印

为你推荐

  • 读书简介
  • 目录
  • 累计评论(0条)
  • 读书简介
  • 目录
  • 累计评论(0条)
Practically and deeply understand concurrency in Python to write efficient programs About This Book ? Build highly efficient, robust, and concurrent applications ? Work through practical examples that will help you address the challenges of writing concurrent code ? Improve the overall speed of execution in multiprocessor and multicore systems and keep them highly available Who This Book Is For This book is for Python developers who would like to get started with concurrent programming. Readers are expected to have a working knowledge of the Python language, as this book will build on these fundamentals concepts. What You Will Learn ? Explore the concept of threading and multiprocessing in Python ? Understand concurrency with threads ? Manage exceptions in child threads ? Handle the hardest part in a concurrent system — shared resources ? Build concurrent systems with Communicating Sequential Processes (CSP) ? Maintain all concurrent systems and master them ? Apply reactive programming to build concurrent systems ? Use GPU to solve specific problems In Detail Python is a very high level, general purpose language that is utilized heavily in fields such as data science and research, as well as being one of the top choices for general purpose programming for programmers around the world. It features a wide number of powerful, high and low-level libraries and frameworks that complement its delightful syntax and enable Python programmers to create. This book introduces some of the most popular libraries and frameworks and goes in-depth into how you can leverage these libraries for your own high-concurrent, highly-performant Python programs. We'll cover the fundamental concepts of concurrency needed to be able to write your own concurrent and parallel software systems in Python. The book will guide you down the path to mastering Python concurrency, giving you all the necessary hardware and theoretical knowledge. We'll cover concepts such as debugging and exception handling as well as some of the most popular libraries and frameworks that allow you to create event-driven and reactive systems. By the end of the book, you'll have learned the techniques to write incredibly efficient concurrent systems that follow best practices. Style and approach This easy-to-follow guide teaches you new practices and techniques to optimize your code, and then moves toward more advanced ways to effectively write efficient Python code. Small and simple practical examples will help you test the concepts yourself, and you will be able to easily adapt them for any application.
目录展开

Title Page

Copyright

Learning Concurrency in Python

Credits

About the Author

About the Reviewer

www.PacktPub.com

Why subscribe?

Customer Feedback

Preface

What this book covers

What you need for this book

Who this book is for

Conventions

Reader feedback

Downloading the example code

Errata

Piracy

Questions

Speed It Up!

History of concurrency

Threads and multithreading

What is a thread?

Types of threads

What is multithreading?

Processes

Properties of processes

Multiprocessing

Event-driven programming

Turtle

Breaking it down

Reactive programming

ReactiveX - RxPy

Breaking it down

GPU programming

PyCUDA

OpenCL

Theano

The limitations of Python

Jython

IronPython

Why should we use Python?

Concurrent image download

Sequential download

Breaking it down

Concurrent download

Breaking it down

Improving number crunching with multiprocessing

Sequential prime factorization

Breaking it down

Concurrent prime factorization

Breaking it down

Summary

Parallelize It

Understanding concurrency

Properties of concurrent systems

I/O bottlenecks

Understanding parallelism

CPU-bound bottlenecks

How do they work on a CPU?

Single-core CPUs

Clock rate

Martelli model of scalability

Time-sharing - the task scheduler

Multi-core processors

System architecture styles

SISD

SIMD

MISD

MIMD

Computer memory architecture styles

UMA

NUMA

Summary

Life of a Thread

Threads in Python

Thread state

State flow chart

Python example of thread state

Breaking it down

Different types of threads

POSIX threads

Windows threads

The ways to start a thread

Starting a thread

Inheriting from the thread class

Breaking it down

Forking

Example

Breaking it down

Daemonizing a thread

Example

Breaking it down

Handling threads in Python

Starting loads of threads

Example

Breaking it down

Slowing down programs using threads

Example

Breaking it down

Getting the total number of active threads

Example

Breaking it down

Getting the current thread

Example

Breaking it down

Main thread

Example

Breaking it down

Enumerating all threads

Example

Breaking it down

Identifying threads

Example

Breakdown

Ending a thread

Best practice in stopping threads

Example

Output

Orphan processes

How does the operating system handle threads

Creating processes versus threads

Example

Breaking it down

Multithreading models

One-to-one thread mapping

Many-to-one

Many-to-many

Summary

Synchronization between Threads

Synchronization between threads

The Dining Philosophers

Example

Output

Race conditions

Process execution sequence

The solution

Critical sections

Filesystem

Life-critical systems

Shared resources and data races

The join method

Breaking it down

Putting it together

Locks

Example

Breaking it down

RLocks

Example

Breaking it down

Output

RLocks versus regular locks

Condition

Definition

Example

Our publisher

Our subscriber

Kicking it off

The results

Semaphores

Class definition

Example

The TicketSeller class

Output

Thread race

Bounded semaphores

Events

Example

Breaking it down

Barriers

Example

Breaking it down

Output

Summary

Communication between Threads

Standard data structures

Sets

Extending the class

Exercise - extending other primitives

Decorator

Class decorator

Lists

Queues

FIFO queues

Example

Breaking it down

Output

LIFO queues

Example

Breaking it down

Output

PriorityQueue

Example

Breakdown

Output

Queue objects

Full/empty queues

Example

Output

The join() function

Example

Breakdown

Output

Deque objects

Example

Breakdown

Output

Appending elements

Example

Breaking it down

Output

Popping elements

Example

Breaking it down

Output

Inserting elements

Example

Breaking it down

Output

Rotation

Example

Breaking it down

Output

Defining your own thread-safe communication structures

A web Crawler example

Requirements

Design

Our Crawler class

Our starting point

Extending the queue object

Breaking it down

Output

Future enhancements

Conclusion

Exercise - testing your skills

Summary

Debug and Benchmark

Testing strategies

Why do we test?

Testing concurrent software systems

What should we test?

Unit tests

PyUnit

Example

Output

Expanding our test suite

Unit testing concurrent code

Integration tests

Debugging

Make it work as a single thread

Pdb

An interactive example

Catching exceptions in child threads

Benchmarking

The timeit module

Timeit versus time

Command-line example

Importing timeit into your code

Utilizing decorators

Timing context manager

Output

Profiling

cProfile

Simple profile example

The line_profiler tool

Kernprof

Memory profiling

Memory profile graphs

Summary

Executors and Pools

Concurrent futures

Executor objects

Creating a ThreadPoolExecutor

Example

Output

Context manager

Example

Output

Maps

Example

Output

Shutdown of executor objects

Example

Output

Future objects

Methods in future objects

The result() method

The add_done_callback() method

The .running() method

The cancel() method

The .exception() method

The .done() method

Unit testing future objects

The set_running_or_notify_cancel() method

The set_result() method

The set_exception() method

Cancelling callable

Example

Output

Getting the result

Example

Output

Using as_completed

Example

Output

Setting callbacks

Example

Output

Chaining callbacks

Exception classes

Example

Output

ProcessPoolExecutor

Creating a ProcessPoolExecutor

Example

Output

Context Manager

Example

Output

Exercise

Getting started

Improving the speed of computationally bound problems

Full code sample

Output

Improving our crawler

The plan

New improvements

Refactoring our code

Storing the results in a CSV file

Exercise - capture more info from each page crawl

concurrent.futures in Python 2.7

Summary

Multiprocessing

Working around the GIL

Utilizing sub-processes

Example

Output

The life of a process

Starting a process using fork

Spawning a process

Forkserver

Daemon processes

Example

Breaking it down

Output

Identifying processes using PIDs

Example

Output

Terminating a process

Example

Getting the current process

Subclassing processes

Example

Output

Multiprocessing pools

The difference between concurrent.futures.ProcessPoolExecutor and Pool

Context manager

Example

Output

Submitting tasks to a process pool

Apply

Apply_async

Map

Map_async

Imap

Imap_unordered

Starmap

Starmap_async

Maxtasksperchild

Communication between processes

Pipes

Anonymous pipes

Named pipes

Working with pipes

Example

Handling Exceptions

Using pipes

Multiprocessing managers

Namespaces

Example

Queues

Example

Output

Listeners and clients

Example

The Listener class

The Client class

Output

Logging

Example

Communicating sequential processes

PyCSP

Processes in PyCSP

Output

Summary

Event-Driven Programming

Event-driven programming

The event loop

Asyncio

Getting started

Event loops

The run_forever() method

The run_until_complete() method

The stop() method

The is_closed() method

The close() function

Tasks

Example

The all_tasks(loop=None) method

The current_tasks() function

The cancel() function

Task functions

The as_completed(fs, *, loop=None, timeout=None) function

The ensure_future(coro_or_future, *, loop=None) function

The wrap_future(future, *, loop=None) function

The gather(*coroes_or_futures, loop=None, return_exceptions=False) function

The wait() function

Futures

Example

Output

Coroutines

Chaining coroutines

Output

Transports

Protocols

Synchronization between coroutines

Locks

Queues

Events and conditions

Semaphores and BoundedSemaphores

Sub-processes

Debugging asyncio programs

Debug mode

Twisted

A simple web server example

Gevent

Event loops

Greenlets

Simple example-hostnames

Output

Monkey patching

Summary

Reactive Programming

Basic reactive programming

Maintaining purity

ReactiveX, or RX

Installing RxPY

Observables

Creating observers

Example

Example 2

Breaking it down

Output

Lambda functions

Example

Breaking it down

On_next, on_completed, and on_error in lambda form

Output

Operators and chaining

Filter example

Breaking it down

Chained operators

The different operators

Creating observables

Transforming observables

Filtering observables

Error-handling observables

Hot and cold observables

Emitting events

Example

Breaking it down

Output

Multicasting

Example

Output

Combining observables

Zip() example

Output

The merge_all() operator

Output

Concurrency

Example

Output

PyFunctional

Installation and official docs

Simple example

Output

Streams, transformations, and actions

Filtering lists

Output

Reading/writing SQLite3

Compressed files

Parallel execution

Summary

Using the GPU

Basic reactive programming

Maintaining purity

ReactiveX, or RX

Installing RxPY

Observables

Creating observers

Example

Example 2

Breaking it down

Output

Lambda functions

Example

Breaking it down

On_next, on_completed, and on_error in lambda form

Output

Operators and chaining

Filter example

Breaking it down

Chained operators

The different operators

Creating observables

Transforming observables

Filtering observables

Error-handling observables

Hot and cold observables

Emitting events

Example

Breaking it down

Output

Multicasting

Example

Output

Combining observables

Zip() example

Output

The merge_all() operator

Output

Concurrency

Example

Output

PyFunctional

Installation and official docs

Simple example

Output

Streams, transformations, and actions

Filtering lists

Output

Reading/writing SQLite3

Compressed files

Parallel execution

Summary

Choosing a Solution

Libraries not covered in this book

GPU

PyGPU

Event-driven and reactive libraries

Tornado

Flask

Celery

Data science

Pandas

Matplotlib

TensorFlow

Designing your systems

Requirements

Functional requirements

Non-functional requirements

Design

Computationally expensive

Event-heavy applications

I/O-heavy applications

Recommended design books

Software Architecture with Python

Python: Master the Art of Design Patterns

Research

Summary

累计评论(0条) 1个书友正在讨论这本书 发表评论

发表评论

发表评论,分享你的想法吧!

买过这本书的人还买过

读了这本书的人还在读

回顶部