Python Concurrency with asyncio

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Learn how to speed up slow Python code with concurrent programming and the cutting-edge asyncio library. • Use coroutines and tasks alongside async/await syntax to run code concurrently • Build web APIs and make concurrency web requests with aiohttp • Run thousands of SQL queries concurrently • Create a map-reduce job that can process gigabytes of data concurrently • Use threading with asyncio to mix blocking code with asyncio code Python is flexible, versatile, and easy to learn. It can also be very slow compared to lower-level languages. Python Concurrency with asyncio teaches you how to boost Python's performance by applying a variety of concurrency techniques. You'll learn how the complex-but-powerful asyncio library can achieve concurrency with just a single thread and use asyncio's APIs to run multiple web requests and database queries simultaneously. The book covers using asyncio with the entire Python concurrency landscape, including multiprocessing and multithreading. About the technology It’s easy to overload standard Python and watch your programs slow to a crawl. The asyncio library was built to solve these problems by making it easy to divide and schedule tasks. It seamlessly handles multiple operations concurrently, leading to apps that are lightning fast and scalable. About the book Python Concurrency with asyncio introduces asynchronous, parallel, and concurrent programming through hands-on Python examples. Hard-to-grok concurrency topics are broken down into simple flowcharts that make it easy to see how your tasks are running. You’ll learn how to overcome the limitations of Python using asyncio to speed up slow web servers and microservices. You’ll even combine asyncio with traditional multiprocessing techniques for huge improvements to performance. What's inside • Build web APIs and make concurrency web requests with aiohttp • Run thousands of SQL queries concurrently • Create a map-reduce job that can process gigabytes of data concurrently • Use threading with asyncio to mix blocking code with asyncio code About the reader For intermediate Python programmers. No previous experience of concurrency required. About the author Matthew Fowler has over 15 years of software engineering experience in roles from architect to engineering director.

Author(s): Matthew Fowler
Edition: 1
Publisher: Manning Publications
Year: 2022

Language: English
Commentary: Vector PDF
Pages: 376
City: Shelter Island, NY
Tags: Web Programming; Python; Web Applications; Concurrency; Asynchronous Programming; Relational Databases; PostgreSQL; Stream Processing; Microservices; MapReduce; Generators; Coroutines; asyncio; Queues; Multitasking

Python Concurrency with asyncio
contents
preface
acknowledgments
about this book
Who should read this book?
How this book is organized: A road map
About the code
liveBook discussion forum
about the author
about the cover illustration
1 Getting to know asyncio
1.1 What is asyncio?
1.2 What is I/O-bound and what is CPU-bound?
1.3 Understanding concurrency, parallelism, and multitasking
1.3.1 Concurrency
1.3.2 Parallelism
1.3.3 The difference between concurrency and parallelism
1.3.4 What is multitasking?
1.3.5 The benefits of cooperative multitasking
1.4 Understanding processes, threads, multithreading, and multiprocessing
1.4.1 Process
1.4.2 Thread
1.5 Understanding the global interpreter lock
1.5.1 Is the GIL ever released?
1.5.2 asyncio and the GIL
1.6 How single-threaded concurrency works
1.6.1 What is a socket?
1.7 How an event loop works
Summary
2 asyncio basics
2.1 Introducing coroutines
2.1.1 Creating coroutines with the async keyword
2.1.2 Pausing execution with the await keyword
2.2 Introducing long-running coroutines with sleep
2.3 Running concurrently with tasks
2.3.1 The basics of creating tasks
2.3.2 Running multiple tasks concurrently
2.4 Canceling tasks and setting timeouts
2.4.1 Canceling tasks
2.4.2 Setting a timeout and canceling with wait_for
2.5 Tasks, coroutines, futures, and awaitables
2.5.1 Introducing futures
2.5.2 The relationship between futures, tasks, and coroutines
2.6 Measuring coroutine execution time with decorators
2.7 The pitfalls of coroutines and tasks
2.7.1 Running CPU-bound code
2.7.2 Running blocking APIs
2.8 Accessing and manually managing the event loop
2.8.1 Creating an event loop manually
2.8.2 Accessing the event loop
2.9 Using debug mode
2.9.1 Using asyncio.run
2.9.2 Using command-line arguments
2.9.3 Using environment variables
Summary
3 A first asyncio application
3.1 Working with blocking sockets
3.2 Connecting to a server with Telnet
3.2.1 Reading and writing data to and from a socket
3.2.2 Allowing multiple connections and the dangers of blocking
3.3 Working with non-blocking sockets
3.4 Using the selectors module to build a socket event loop
3.5 An echo server on the asyncio event loop
3.5.1 Event loop coroutines for sockets
3.5.2 Designing an asyncio echo server
3.5.3 Handling errors in tasks
3.6 Shutting down gracefully
3.6.1 Listening for signals
3.6.2 Waiting for pending tasks to finish
Summary
4 Concurrent web requests
4.1 Introducing aiohttp
4.2 Asynchronous context managers
4.2.1 Making a web request with aiohttp
4.2.2 Setting timeouts with aiohttp
4.3 Running tasks concurrently, revisited
4.4 Running requests concurrently with gather
4.4.1 Handling exceptions with gather
4.5 Processing requests as they complete
4.5.1 Timeouts with as_completed
4.6 Finer-grained control with wait
4.6.1 Waiting for all tasks to complete
4.6.2 Watching for exceptions
4.6.3 Processing results as they complete
4.6.4 Handling timeouts
4.6.5 Why wrap everything in a task?
Summary
5 Non-blocking database drivers
5.1 Introducing asyncpg
5.2 Connecting to a Postgres database
5.3 Defining a database schema
5.4 Executing queries with asyncpg
5.5 Executing queries concurrently with connection pools
5.5.1 Inserting random SKUs into the product database
5.5.2 Creating a connection pool to run queries concurrently
5.6 Managing transactions with asyncpg
5.6.1 Nested transactions
5.6.2 Manually managing transactions
5.7 Asynchronous generators and streaming result sets
5.7.1 Introducing asynchronous generators
5.7.2 Using asynchronous generators with a streaming cursor
Summary
6 Handling CPU-bound work
6.1 Introducing the multiprocessing library
6.2 Using process pools
6.2.1 Using asynchronous results
6.3 Using process pool executors with asyncio
6.3.1 Introducing process pool executors
6.3.2 Process pool executors with the asyncio event loop
6.4 Solving a problem with MapReduce using asyncio
6.4.1 A simple MapReduce example
6.4.2 The Google Books Ngram dataset
6.4.3 Mapping and reducing with asyncio
6.5 Shared data and locks
6.5.1 Sharing data and race conditions
6.5.2 Synchronizing with locks
6.5.3 Sharing data with process pools
6.6 Multiple processes, multiple event loops
Summary
7 Handling blocking work with threads
7.1 Introducing the threading module
7.2 Using threads with asyncio
7.2.1 Introducing the requests library
7.2.2 Introducing thread pool executors
7.2.3 Thread pool executors with asyncio
7.2.4 Default executors
7.3 Locks, shared data, and deadlocks
7.3.1 Reentrant locks
7.3.2 Deadlocks
7.4 Event loops in separate threads
7.4.1 Introducing Tkinter
7.4.2 Building a responsive UI with asyncio and threads
7.5 Using threads for CPU-bound work
7.5.1 Multithreading with hashlib
7.5.2 Multithreading with NumPy
Summary
8 Streams
8.1 Introducing streams
8.2 Transports and protocols
8.3 Stream readers and stream writers
8.4 Non-blocking command-line input
8.4.1 Terminal raw mode and the read coroutine
8.5 Creating servers
8.6 Creating a chat server and client
Summary
9 Web applications
9.1 Creating a REST API with aiohttp
9.1.1 What is REST?
9.1.2 aiohttp server basics
9.1.3 Connecting to a database and returning results
9.1.4 Comparing aiohttp with Flask
9.2 The asynchronous server gateway interface
9.2.1 How does ASGI compare to WSGI?
9.3 ASGI with Starlette
9.3.1 A REST endpoint with Starlette
9.3.2 WebSockets with Starlette
9.4 Django asynchronous views
9.4.1 Running blocking work in an asynchronous view
9.4.2 Using async code in synchronous views
Summary
10 Microservices
10.1 Why microservices?
10.1.1 Complexity of code
10.1.2 Scalability
10.1.3 Team and stack independence
10.1.4 How can asyncio help?
10.2 Introducing the backend-for-frontend pattern
10.3 Implementing the product listing API
10.3.1 User favorite service
10.3.2 Implementing the base services
10.3.3 Implementing the backend-for-frontend service
10.3.4 Retrying failed requests
10.3.5 The circuit breaker pattern
Summary
11 Synchronization
11.1 Understanding single-threaded concurrency bugs
11.2 Locks
11.3 Limiting concurrency with semaphores
11.3.1 Bounded semaphores
11.4 Notifying tasks with events
11.5 Conditions
Summary
12 Asynchronous queues
12.1 Asynchronous queue basics
12.1.1 Queues in web applications
12.1.2 A web crawler queue
12.2 Priority queues
12.3 LIFO queues
Summary
13 Managing subprocesses
13.1 Creating a subprocess
13.1.1 Controlling standard output
13.1.2 Running subprocesses concurrently
13.2 Communicating with subprocesses
Summary
14 Advanced asyncio
14.1 APIs with coroutines and functions
14.2 Context variables
14.3 Forcing an event loop iteration
14.4 Using different event loop implementations
14.5 Creating a custom event loop
14.5.1 Coroutines and generators
14.5.2 Generator-based coroutines are deprecated
14.5.3 Custom awaitables
14.5.4 Using sockets with futures
14.5.5 A task implementation
14.5.6 Implementing an event loop
14.5.7 Implementing a server with a custom event loop
Summary
index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W