Mastering Concurrency in Python

Book description

Immerse yourself in the world of Python concurrency and tackle the most complex concurrent programming problems

Key Features

  • Explore the core syntaxes, language features and modern patterns of concurrency in Python
  • Understand how to use concurrency to keep data consistent and applications responsive
  • Utilize application scaffolding to design highly-scalable programs

Book Description

Python is one of the most popular programming languages, with numerous libraries and frameworks that facilitate high-performance computing. Concurrency and parallelism in Python are essential when it comes to multiprocessing and multithreading; they behave differently, but their common aim is to reduce the execution time. This book serves as a comprehensive introduction to various advanced concepts in concurrent engineering and programming.

Mastering Concurrency in Python starts by introducing the concepts and principles in concurrency, right from Amdahl's Law to multithreading programming, followed by elucidating multiprocessing programming, web scraping, and asynchronous I/O, together with common problems that engineers and programmers face in concurrent programming. Next, the book covers a number of advanced concepts in Python concurrency and how they interact with the Python ecosystem, including the Global Interpreter Lock (GIL). Finally, you'll learn how to solve real-world concurrency problems through examples.

By the end of the book, you will have gained extensive theoretical knowledge of concurrency and the ways in which concurrency is supported by the Python language

What you will learn

  • Explore the concepts of concurrency in programming
  • Explore the core syntax and features that enable concurrency in Python
  • Understand the correct way to implement concurrency
  • Abstract methods to keep the data consistent in your program
  • Analyze problems commonly faced in concurrent programming
  • Use application scaffolding to design highly-scalable programs

Who this book is for

This book is for developers who wish to build high-performance applications and learn about signle-core, multicore programming or distributed concurrency. Some experience with Python programming language is assumed.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Concurrency in Python
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Code in Action
      4. Conventions used
    4. Get in touch
      1. Reviews
  7. Advanced Introduction to Concurrent and Parallel Programming
    1. Technical requirements
    2. What is concurrency?
      1. Concurrent versus sequential
      2. Example 1 – checking whether a non-negative number is prime
      3. Concurrent versus parallel
        1. A quick metaphor
    3. Not everything should be made concurrent
      1. Embarrassingly parallel
      2. Inherently sequential
        1. Example 2 – inherently sequential tasks
      3. I/O bound
    4. The history, present, and future of concurrency
      1. The history of concurrency
      2. The present
      3. The future
    5. A brief overview of mastering concurrency in Python
      1. Why Python?
    6. Setting up your Python environment
      1. General setup
      2. Downloading example code
    7. Summary
    8. Questions
    9. Further reading
  8. Amdahl's Law
    1. Technical requirements
    2. Amdahl's Law
      1. Terminology
    3. Formula and interpretation
      1. The formula for Amdahl's Law
        1. A quick example
      2. Implications
    4. Amdahl's Law's relationship to the law of diminishing returns
    5. How to simulate in Python
    6. Practical applications of Amdahl's Law
    7. Summary
    8. Questions
    9. Further reading
  9. Working with Threads in Python
    1. Technical requirements
    2. The concept of a thread
      1. Threads versus processes
      2. Multithreading
      3. An example in Python
    3. An overview of the threading module
      1. The thread module in Python 2
      2. The threading module in Python 3
    4. Creating a new thread in Python
      1. Starting a thread with the thread module
      2. Starting a thread with the threading module
    5. Synchronizing threads
      1. The concept of thread synchronization
      2. The threading.Lock class
      3. An example in Python
    6. Multithreaded priority queue
      1. A connection between real-life and programmatic queues
      2. The queue module
      3. Queuing in concurrent programming
      4. Multithreaded priority queue
    7. Summary
    8. Questions
    9. Further reading
  10. Using the with Statement in Threads
    1. Technical requirements
    2. Context management
      1. Starting from managing files
      2. The with statement as a context manager
      3. The syntax of the with statement
    3. The with statement in concurrent programming
      1. Example of deadlock handling
    4. Summary
    5. Questions
    6. Further reading
  11. Concurrent Web Requests
    1. Technical requirements
    2. The basics of web requests
      1. HTML
      2. HTTP requests
      3. HTTP status code
    3. The requests module
      1. Making a request in Python
      2. Running a ping test
    4. Concurrent web requests
      1. Spawning multiple threads
      2. Refactoring request logic
    5. The problem of timeout
      1. Support from httpstat.us and simulation in Python
      2. Timeout specifications
    6. Good practices in making web requests
      1. Consider the terms of service and data-collecting policies
      2. Error handling
      3. Update your program regularly
      4. Avoid making a large number of requests
    7. Summary
    8. Questions
    9. Further reading
  12. Working with Processes in Python
    1. Technical requirements
    2. The concept of a process
      1. Processes versus threads
      2. Multiprocessing
      3. Introductory example in Python
    3. An overview of the multiprocessing module
      1. The process class
      2. The Pool class
      3. Determining the current process, waiting, and terminating processes
        1. Determining the current process
        2. Waiting for processes
        3. Terminating processes
    4. Interprocess communication
      1. Message passing for a single worker
      2. Message passing between several workers
    5. Summary
    6. Questions
    7. Further reading
  13. Reduction Operators in Processes
    1. Technical requirements
    2. The concept of reduction operators
      1. Properties of a reduction operator
      2. Examples and non-examples
    3. Example implementation in Python
    4. Real-life applications of concurrent reduction operators
    5. Summary
    6. Questions
    7. Further reading
  14. Concurrent Image Processing
    1. Technical requirements
    2. Image processing fundamentals
      1. Python as an image processing tool
        1. Installing OpenCV and NumPy
      2. Computer image basics
        1. RGB values
        2. Pixels and image files
        3. Coordinates inside an image
      3. OpenCV API
      4. Image processing techniques
        1. Grayscaling
        2. Thresholding
    3. Applying concurrency to image processing
    4. Good concurrent image processing practices
      1. Choosing the correct way (out of many)
      2. Spawning an appropriate number of processes
      3. Processing input/output concurrently
    5. Summary
    6. Questions
    7. Further reading
  15. Introduction to Asynchronous Programming
    1. Technical requirements
    2. A quick analogy
    3. Asynchronous versus other programming models
      1. Asynchronous versus synchronous programming
      2. Asynchronous versus threading and multiprocessing
    4. An example in Python
    5. Summary
    6. Questions
    7. Further reading
  16. Implementing Asynchronous Programming in Python
    1. Technical requirements
    2. The asyncio module
      1. Coroutines, event loops, and futures
      2. Asyncio API
    3. The asyncio framework in action
      1. Asynchronously counting down
      2. A note about blocking functions
      3. Asynchronous prime-checking
      4. Improvements from Python 3.7
      5. Inherently blocking tasks
    4. concurrent.futures as a solution for blocking tasks
      1. Changes in the framework
      2. Examples in Python
    5. Summary
    6. Questions
    7. Further reading
  17. Building Communication Channels with asyncio
    1. Technical requirements
    2. The ecosystem of communication channels
      1. Communication protocol layers
      2. Asynchronous programming for communication channels
      3. Transports and protocols in asyncio
      4. The big picture of asyncio's server client
    3. Python example
      1. Starting a server
      2. Installing Telnet
      3. Simulating a connection channel
      4. Sending messages back to clients
      5. Closing the transports
    4. Client-side communication with aiohttp
      1. Installing aiohttp and aiofiles
      2. Fetching a website's HTML code
      3. Writing files asynchronously
    5. Summary
    6. Questions
    7. Further reading
  18. Deadlocks
    1. Technical requirements
    2. The concept of deadlock
      1. The Dining Philosophers problem
      2. Deadlock in a concurrent system
      3. Python simulation
    3. Approaches to deadlock situations
      1. Implementing ranking among resources
      2. Ignoring locks and sharing resources
        1. An additional note about locks
        2. Concluding note on deadlock solutions
    4. The concept of livelock
    5. Summary
    6. Questions
    7. Further reading
  19. Starvation
    1. Technical requirements
    2. The concept of starvation
      1. What is starvation?
      2. Scheduling
      3. Causes of starvation
      4. Starvation's relationship to deadlock
    3. The readers-writers problem
      1. Problem statement
      2. The first readers-writers problem
      3. The second readers-writers problem
      4. The third readers-writers problem
    4. Solutions to starvation
    5. Summary
    6. Questions
    7. Further reading
  20. Race Conditions
    1. Technical requirements
    2. The concept of race conditions
      1. Critical sections
      2. How race conditions occur
    3. Simulating race conditions in Python
    4. Locks as a solution to race conditions
      1. The effectiveness of locks
      2. Implementation in Python
      3. The downside of locks
        1. Turning a concurrent program sequential
        2. Locks do not lock anything
    5. Race conditions in real life
      1. Security
      2. Operating systems
      3. Networking
    6. Summary
    7. Questions
    8. Further reading
  21. The Global Interpreter Lock
    1. Technical requirements
    2. An introduction to the Global Interpreter Lock
      1. An analysis of memory management in Python
      2. The problem that the GIL addresses
      3. Problems raised by the GIL
    3. The potential removal of the GIL from Python
    4. How to work with the GIL
      1. Implementing multiprocessing, rather than multithreading 
      2. Getting around the GIL with native extensions
      3. Utilizing a different Python interpreter
    5. Summary
    6. Questions
    7. Further reading
  22. Designing Lock-Based and Mutex-Free Concurrent Data Structures
    1. Technical requirements
    2. Lock-based concurrent data structures in Python
      1. LocklessCounter and race conditions
      2. Embedding locks in the data structure of the counter
      3. The concept of scalability
      4. Analysis of the scalability of the counter data structure
      5. Approximate counters as a solution for scalability
        1. The idea behind approximate counters
        2. Implementing approximate counters in Python
        3. A few considerations for approximate counter designs
    3. Mutex-free concurrent data structures in Python
      1. The impossibility of being lock-free in Python
      2. Introduction to the network data structure
      3. Implementing a simple network data structure in Python and race conditions
      4. RCU as a solution
    4. Building on simple data structures
    5. Summary
    6. Questions
    7. Further reading
  23. Memory Models and Operations on Atomic Types
    1. Technical requirements
    2. Python memory model
      1. The components of Python memory manager
      2. Memory model as a labeled directed graph
      3. In the context of concurrency
    3. Atomic operations in Python
      1. What does it mean to be atomic?
      2. The GIL reconsidered
      3. Innate atomicity in Python
        1. Atomic versus nonatomic
        2. Simulation in Python
    4. Summary
    5. Questions
    6. Further reading
  24. Building a Server from Scratch
    1. Technical requirements
    2. Low-level network programming via the socket module
      1. The theory of server-side communication
      2. The API of the socket module
      3. Building a simple echo server
    3. Building a calculator server with the socket module
      1. The underlying calculation logic
      2. Implementing the calculator server
    4. Building a non-blocking server
      1. Analyzing the concurrency of the server
      2. Generators in Python
      3. Asynchronous generators and the send method
      4. Making the server non-blocking
    5. Summary
    6. Questions
    7. Further reading
  25. Testing, Debugging, and Scheduling Concurrent Applications
    1. Technical requirements
    2. Scheduling with APScheduler
      1. Installing APScheduler
      2. Not a scheduling service
      3. APScheduler functionalities
      4. APScheduler API
        1. Scheduler classes
        2. Executor classes
        3. Trigger keywords
        4. Common scheduler methods
      5. Examples in Python
        1. Blocking scheduler
        2. Background scheduler
        3. Executor pool
        4. Running on the cloud
    3. Testing and concurrency in Python
      1. Testing concurrent programs
        1. Unit testing
        2. Static code analysis
      2. Testing programs concurrently
    4. Debugging concurrent programs
      1. Debugging tools and techniques
      2. Debugging and concurrency
    5. Summary
    6. Questions
    7. Further reading
  26. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Chapter 18
    19. Chapter 19
  27. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering Concurrency in Python
  • Author(s): Quan Nguyen
  • Release date: November 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789343052