Chapter 21. Data in Time: Concurrency
All we have to decide is what to do with the time that is given us.
J. R. R. Tolkien
This chapter and the next two are longer and a bit more challenging than earlier ones: data in time (sequential and concurrent access on a single computer), then data in space (networking), and finally data in a box (storage and retrieval with special files and databases).
Programs and Processes
When you run an individual program, your operating system creates a single process. It uses system resources (CPU, memory, disk space) and data structures in the operating system’s kernel (file and network connections, usage statistics, and so on). A process is isolated from other processes; it can’t see what other processes are doing or interfere with them.
The operating system keeps track of all the running processes,
giving each a little time to run and then switching to another,
with the twin goals of spreading the work around fairly
and being responsive
to the user.
You can see the state of your processes with
graphical interfaces such as the Mac’s Activity Monitor (macOS),
Task Manager on Windows-based computers,
or the top command in Linux.
You can also access process data from your own programs.
The standard library’s os module provides a common way of
accessing some system information.
For instance, the following functions get the process ID
and the current working directory
of the running Python interpreter:
>>>importos>>>os.getpid()76051>>>os
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access