Python_Concurrency_with_asyncio_Matthew_Fowler.pdf

(6217 KB) Pobierz
Matthew Fowler
MANNING
I want to . . .
Learn the basics of single-threaded concurrency
How?
Understand how selectors and the event loop
work
Use asyncio tasks and API functions
Use the task’s
wait_for
method
Use the task’s
cancel
method
Create a non-blocking echo server with selectors
Use the signals API (*nix systems only)
Use asyncio API functions such as
gather,
wait,
and
as_completed
Use a library, such as aiohttp, with asyncio API
functions
Use an asynchronous library like asyncpg with
asyncio API functions and connection pools
Use multiprocessing and process pools
Save the state to shared memory
Use multiprocessing locks
Use multithreading and thread pools
Chapter(s)
1, 3
Run two operations concurrently
Add a timeout to a long-running task
Cancel a long-running task
Learn how non-blocking sockets work
Handle graceful shutdowns of asyncio applications
Run multiple tasks concurrently
2, 4
2
2
3
3
4
Run asynchronous web API requests
4
Run multiple SQL queries concurrently
5
Run CPU-intensive work concurrently
Use shared state among multiple processes
Avoid race conditions in multiprocessing code
Run blocking I/O-based APIs, such as requests,
concurrently
Avoid race conditions and deadlocks in multi-
threading code
Build a responsive GUI with asyncio
Run multiple CPU-intensive tasks, such as data
analysis with NumPy, concurrently
Build a non-blocking command line application
6
6
6
7
Use multithreading locks and reentrant locks
7
Use multithreading with threading queues
Use multiprocessing, multithreading in certain
circumstances
Use streams to asynchronously read data
(*nix systems only)
Use a web framework with ASGI support such as
Starlette or Django
Use the WebSocket library
Use asyncio API functions with retries and the
circuit breaker pattern
Use asyncio locks
Use asyncio semaphores
Use asyncio events
7
6, 7
8
Build a web application with asyncio
9
Use WebSockets asynchronously
Build a resilient backend-for-frontend microservice
architecture with async concepts
Prevent single-threaded race conditions
Limit the number of tasks running concurrently
Wait until an event occurs before performing
an operation
Control access to a shared resource
9
10
11
11
11
Use asyncio condtions
11
Continued on inside back cover
Python Concurrency
with asyncio
M
ATTHEW
F
OWLER
MANNING
S
HELTER
I
SLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com.
The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
©2022 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning Publications
was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
The author and publisher have made every effort to ensure that the information in this book
was correct at press time. The author and publisher do not assume and hereby disclaim any
liability to any party for any loss, damage, or disruption caused by errors or omissions, whether
such errors or omissions result from negligence, accident, or any other cause, or from any usage
of the information herein.
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Development editor:
Technical development editor:
Review editor:
Production editor:
Copy editor:
Proofreader:
Technical proofreader:
Typesetter:
Cover designer:
Doug Rudder
Robert Wenner
Mihaela Batinic
´
Andy Marinkovich
Christian Berk
Keri Hales
Mayur Patil
Dennis Dalinnik
Marija Tudor
ISBN: 9781617298660
Printed in the United States of America
To my beautiful wife Kathy,
thank you for always being there.
Zgłoś jeśli naruszono regulamin