Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Asynchronous Programming in Python
  • Table Of Contents Toc
  • Feedback & Rating feedback
Asynchronous Programming in Python

Asynchronous Programming in Python

By : Nicolas Bohorquez
close
close
Asynchronous Programming in Python

Asynchronous Programming in Python

By: Nicolas Bohorquez

Overview of this book

Asynchronous programming is one of the most effective but often misunderstood techniques for building fast, scalable, and responsive systems in Python. While it can significantly improve performance, efficiency, and sustainability, using async without a clear understanding of its trade-offs can lead to fragile designs and hard-to-debug issues. This book offers a structured approach to applying asynchronous programming in Python. It begins with a conceptual framework to help you distinguish between synchronous and asynchronous execution models, and shows how async relates to other concurrency strategies such as multithreading and multiprocessing. From there, you will explore the core tools available for building async applications in Python. You will also learn how to measure the impact of async programming in practical scenarios, profile and debug asynchronous code, and evaluate performance improvements using real-world metrics. The final chapters focus on applying async techniques to common cloud-based systems, such as web frameworks, database interactions, and data-pipelines tools. Designed for developers looking to apply async programming with confidence, this book blends real-world examples with core concepts to help you write efficient, maintainable Python code.
Table of Contents (14 chapters)
close
close
12
Other Books You May Enjoy
13
Index

Operating system process and threads

Central Processing Units (CPUs) function in a fetch – execute cycle. Specifically, the operating system (OS) fetches a set of instructions (program) from disk into memory, and they are then executed by the CPU. A program being executed is called a process. Loading a program into memory to become a process implies dividing memory into these sections:

  • Text: This section of the memory allocated typically contains compiled code with a static set of instructions
  • Data: Static data and global variables required for a running process
  • Heap: Space reserved for dynamically allocated data structures (non-static, non-global variables)
  • Stack: Local variables used in functions, which, if large enough, can compete with allocated heap space (causing a ‘stack overflow’ or ‘insufficient heap space’ error)

Although in an asynchronous program it may appear that all instructions in the set are being called at exactly the same time, technically each step is broken into blocks which are scheduled to be executed by the OS. Those blocks are executed so fast that it gives the impression that a processor is computing several things at the same time.

The change from execution of code blocks from one process to execution of blocks from another is a costly operation, called context switching. Context switching involves managing interruptions in the processing of a block, knowing the execution status of any given process, and waiting for other processes to complete, among other requirements for proper process flow.

Introducing threads

Modern computers typically have multiple cores, each of which is capable of executing a process. To better handle context switching, an abstraction was created: a thread, or atomic unit of processing. Each thread runs on a single core, and a processor can simultaneously run multiple threads from a single process by taking advantage of this architecture.

Threads are also called lightweight processes, since they must each individually conform to the structure described above for processes, but there is an important consideration: multiple threads of a process share the memory heap and code/data segments, which means that programmers must be careful to ensure that shared resource constraints are adhered to, but each thread maintains its own private stack.

The following diagram shows how processing can vary according to CPU and OS characteristics:

Figure 1.6: A single process/single thread processor on the left and a multithreaded processor on the right

What happens if a process has more threads than available cores? Thread context switching is ‘lighter’ because it involves saving and restoring less state, while process context switching is ‘heavier’ because it involves saving and restoring more state, including memory mappings. Therefore, in terms of efficiency, context switching between threads is generally faster and less resource-intensive than context switching between processes.

Some pieces of software are multiprocessor but not multithreaded, meaning that all processes are single-threaded (synchronous) but they can be split to take advantage of multiple processors.

There are two types of thread: kernel threads and user threads. User threads are created, managed, and bounded via the Application Programming Interface (API) provided by a system’s OS and managed by the individual program being run. The key point about user threads is that if one of them performs blocking operations, the entire process is blocked. This impacts the way multithreaded programs are designed.

The lifecycle of kernel threads, on the other hand, is entirely managed by the operating system. This type of thread has the advantage that if an operation blocks thread execution, the parent process is not blocked. Python’s default threading model is managed by the underlying operating system kernel, even if by default only one thread can run the interpreter at the time. We will explore this design in more detail in Anchor 4.

Processes, kernel threads and user threads are constructs that involve close management of the physical resources of a computer. As you might expect, modern programming languages provide abstractions to efficiently manage these concepts and the underlying resources. In the next section we will discuss three programming concepts central to multitasking: green threads, coroutines and fibers.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Asynchronous Programming in Python
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon