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.
Author(s): Quan Nguyen
Publisher: Packt
Year: 2018
Language: English
Pages: 433
Cover......Page 1
Title Page......Page 2
Copyright and Credits......Page 3
Dedication......Page 4
About Packt......Page 5
Contributors......Page 6
Table of Contents......Page 8
Preface......Page 17
Chapter 1: Advanced Introduction to Concurrent and Parallel Programming......Page 24
Concurrent versus sequential......Page 25
Example 1 – checking whether a non-negative number is prime......Page 26
Concurrent versus parallel......Page 29
Not everything should be made concurrent......Page 30
Inherently sequential......Page 31
Example 2 – inherently sequential tasks......Page 32
The history, present, and future of concurrency......Page 34
The history of concurrency......Page 35
The present......Page 36
The future......Page 38
A brief overview of mastering concurrency in Python......Page 40
Why Python?......Page 41
General setup......Page 43
Downloading example code......Page 44
Summary......Page 45
Further reading......Page 46
Technical requirements......Page 47
Terminology......Page 48
The formula for Amdahl's Law......Page 49
Implications......Page 50
Amdahl's Law's relationship to the law of diminishing returns......Page 51
How to simulate in Python......Page 52
Practical applications of Amdahl's Law......Page 56
Summary......Page 57
Further reading......Page 58
Chapter 3: Working with Threads in Python......Page 59
Threads versus processes......Page 60
Multithreading......Page 61
An example in Python......Page 63
The threading module in Python 3......Page 67
Creating a new thread in Python......Page 68
Starting a thread with the thread module......Page 69
Starting a thread with the threading module......Page 71
The concept of thread synchronization......Page 74
An example in Python......Page 75
A connection between real-life and programmatic queues......Page 77
The queue module......Page 78
Queuing in concurrent programming......Page 79
Multithreaded priority queue......Page 83
Summary......Page 84
Further reading......Page 85
Technical requirements......Page 86
Starting from managing files......Page 87
The with statement as a context manager......Page 88
The with statement in concurrent programming......Page 90
Example of deadlock handling......Page 91
Questions......Page 93
Further reading......Page 94
Technical requirements......Page 95
HTML......Page 96
HTTP requests......Page 98
HTTP status code......Page 99
The requests module......Page 100
Making a request in Python......Page 101
Running a ping test......Page 103
Concurrent web requests......Page 104
Spawning multiple threads......Page 105
Refactoring request logic......Page 107
Support from httpstat.us and simulation in Python......Page 109
Timeout specifications......Page 110
Error handling......Page 114
Avoid making a large number of requests......Page 115
Further reading......Page 117
Chapter 6: Working with Processes in Python......Page 118
The concept of a process......Page 119
Processes versus threads......Page 121
Multiprocessing......Page 122
Introductory example in Python......Page 124
The process class......Page 126
The Pool class......Page 127
Determining the current process......Page 128
Waiting for processes......Page 131
Interprocess communication......Page 134
Message passing for a single worker......Page 135
Message passing between several workers......Page 137
Summary......Page 143
Further reading......Page 144
Technical requirements......Page 145
Properties of a reduction operator......Page 146
Examples and non-examples......Page 147
Example implementation in Python......Page 149
Summary......Page 154
Further reading......Page 155
Technical requirements......Page 156
Python as an image processing tool......Page 157
Installing OpenCV and NumPy......Page 158
RGB values......Page 159
Coordinates inside an image......Page 160
OpenCV API......Page 161
Image processing techniques......Page 163
Grayscaling......Page 164
Thresholding......Page 166
Applying concurrency to image processing......Page 171
Choosing the correct way (out of many)......Page 175
Summary......Page 178
Further reading......Page 179
Technical requirements......Page 180
A quick analogy......Page 181
Asynchronous versus other programming models......Page 182
Asynchronous versus synchronous programming......Page 183
Asynchronous versus threading and multiprocessing......Page 184
An example in Python......Page 185
Questions......Page 188
Further reading......Page 189
Technical requirements......Page 190
Coroutines, event loops, and futures......Page 191
Asyncio API......Page 193
The asyncio framework in action......Page 194
Asynchronously counting down......Page 195
A note about blocking functions......Page 199
Asynchronous prime-checking......Page 200
Improvements from Python 3.7......Page 204
Inherently blocking tasks......Page 205
concurrent.futures as a solution for blocking tasks......Page 206
Examples in Python......Page 207
Summary......Page 211
Questions......Page 212
Further reading......Page 213
Chapter 11: Building Communication Channels with asyncio......Page 214
Communication protocol layers......Page 215
Asynchronous programming for communication channels......Page 217
Transports and protocols in asyncio......Page 218
The big picture of asyncio's server client......Page 220
Starting a server......Page 221
Installing Telnet......Page 223
Simulating a connection channel......Page 224
Sending messages back to clients......Page 225
Closing the transports......Page 226
Client-side communication with aiohttp......Page 228
Fetching a website's HTML code......Page 229
Writing files asynchronously......Page 231
Summary......Page 233
Further reading......Page 234
Technical requirements......Page 235
The Dining Philosophers problem......Page 236
Deadlock in a concurrent system......Page 239
Python simulation......Page 240
Implementing ranking among resources......Page 244
Ignoring locks and sharing resources......Page 250
An additional note about locks......Page 252
The concept of livelock......Page 253
Further reading......Page 256
Technical requirements......Page 257
What is starvation?......Page 258
Scheduling......Page 259
Causes of starvation......Page 260
Starvation's relationship to deadlock......Page 261
Problem statement......Page 262
The first readers-writers problem......Page 263
The second readers-writers problem......Page 267
The third readers-writers problem......Page 270
Solutions to starvation......Page 272
Summary......Page 273
Further reading......Page 274
Technical requirements......Page 275
Critical sections......Page 276
How race conditions occur......Page 277
Simulating race conditions in Python......Page 279
The effectiveness of locks......Page 281
Implementation in Python......Page 283
The downside of locks......Page 284
Turning a concurrent program sequential......Page 285
Locks do not lock anything......Page 287
Security......Page 288
Operating systems......Page 289
Networking......Page 290
Questions......Page 291
Further reading......Page 292
Technical requirements......Page 293
An analysis of memory management in Python......Page 294
The problem that the GIL addresses......Page 297
Problems raised by the GIL......Page 298
How to work with the GIL......Page 300
Implementing multiprocessing, rather than multithreading ......Page 301
Summary......Page 303
Further reading......Page 304
Chapter 16: Designing Lock-Based and Mutex-Free Concurrent Data Structures......Page 305
LocklessCounter and race conditions......Page 306
Embedding locks in the data structure of the counter......Page 309
The concept of scalability......Page 311
Analysis of the scalability of the counter data structure......Page 313
The idea behind approximate counters......Page 316
Implementing approximate counters in Python......Page 318
Mutex-free concurrent data structures in Python......Page 324
The impossibility of being lock-free in Python......Page 325
Introduction to the network data structure......Page 326
Implementing a simple network data structure in Python and race conditions......Page 327
RCU as a solution......Page 331
Building on simple data structures......Page 334
Questions......Page 335
Further reading......Page 336
Technical requirements......Page 337
The components of Python memory manager......Page 338
Memory model as a labeled directed graph......Page 339
In the context of concurrency......Page 341
What does it mean to be atomic?......Page 342
The GIL reconsidered......Page 343
Atomic versus nonatomic......Page 344
Simulation in Python......Page 345
Questions......Page 348
Further reading......Page 349
Technical requirements......Page 350
The theory of server-side communication......Page 351
The API of the socket module......Page 353
Building a simple echo server......Page 354
The underlying calculation logic......Page 358
Implementing the calculator server......Page 359
Analyzing the concurrency of the server......Page 362
Generators in Python......Page 366
Asynchronous generators and the send method......Page 368
Making the server non-blocking......Page 370
Questions......Page 376
Further reading......Page 377
Technical requirements......Page 378
Installing APScheduler......Page 379
APScheduler functionalities......Page 380
Executor classes......Page 382
Common scheduler methods......Page 383
Blocking scheduler......Page 384
Background scheduler......Page 385
Executor pool......Page 386
Running on the cloud......Page 388
Testing and concurrency in Python......Page 389
Unit testing......Page 390
Testing programs concurrently......Page 392
Debugging tools and techniques......Page 396
Debugging and concurrency......Page 397
Summary......Page 398
Questions......Page 399
Further reading......Page 400
Assessments......Page 401
Other Books You May Enjoy......Page 426
Index......Page 429