Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Asynchronous Programming in Python

You're reading from   Asynchronous Programming in Python Apply asyncio in Python to build scalable, high-performance apps across multiple scenarios

Arrow left icon
Product type Paperback
Published in Nov 2025
Publisher Packt
ISBN-13 9781836646617
Length 202 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Nicolas Bohorquez Nicolas Bohorquez
Author Profile Icon Nicolas Bohorquez
Nicolas Bohorquez
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Synchronous and Asynchronous Programming Paradigms FREE CHAPTER 2. Identifying Concurrency and Parallelism 3. Generators and Coroutines 4. Implementing Coroutines with Asyncio and Trio 5. Assessing Common Mistakes in Asynchronous Programming 6. Testing and Asynchronous Design Patterns 7. Asynchronous Programming in Django, Flask and Quart 8. Asynchronous Data Access 9. Asynchronous Data Pipelines 10. Asynchronous Computing with Notebooks 11. Unlock Your Exclusive Benefits 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
You have been reading a chapter from
Asynchronous Programming in Python
Published in: Nov 2025
Publisher: Packt
ISBN-13: 9781836646617
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon