Skip to Content
Python入门指南, 3rd Edition
book

Python入门指南, 3rd Edition

by Bill Lubanovic
September 2025
Intermediate to advanced
660 pages
7h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Python入门指南, 3rd Edition

第 21 章 时间中的数据:并发 时间数据:并发

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我们所要做的就是如何利用时间。

J.托尔金

本 章和接下来的两章比前面的章节更长,也更有挑战性一些:时间中的数据(单台计算机上的顺序访问和并发访问),然后是空间中的数据(Network+),最后是盒子中的数据(使用特殊文件和数据库进行存储和检索)。

程序和进程

当 你运行一个单独的程序时,你的操作系统会创建一个单独的进程。它使用系统资源(CPU、内存、磁盘空间)和 操作系统内核中的数据结构(文件和网络连接、使用统计等)。一个进程与其他进程是隔离的,它看不到其他进程在做什么,也不会干扰其他进程。

操作系统会跟踪所有正在运行的进程,给每个进程一点运行时间,然后切换到另一个进程,这样做的双重目的是公平地分配工作,并对用户做出响应。 你可以通过图形界面查看进程的状态,如 Mac 的活动监视器(macOS)、Windows 计算机上的任务管理器或 Linux 中的top 命令。

您也可以从自己的程序中访问进程数据。标准库的os 模块提供了访问一些系统信息的常用方法。例如,以下函数可以获取运行中的 Python 解释器的进程 ID当前工作目录

>>> import os
>>> os.getpid()
76051
>>> os.getcwd()
'/Users/williamlubanovic'

这些函数还能获取我的用户 ID组 ID

>>> os.getuid()
501
>>> os.getgid()
20

用子进程创建进程

到目前为止,您在这里看到的所有 程序都是单独的进程。您可以使用标准库中的subprocess 模块从 Python 启动和停止其他现有程序。如果您只想在 shell 中运行另一个程序,并获取它所创建的任何输出(包括标准输出和标准错误输出),请使用getoutput() 函数。在这里,我们获取 Unixdate 程序的输出:

>>> import subprocess
>>> ret = subprocess.getoutput('date')
>>> ret
'Thu Jul 31 04:15:51 PM CDT 2025'

在进程结束之前,你不会得到任何返回信息。 如果你需要调用一个可能需要很多时间的程序,请参阅"并发"。由于getoutput() 的参数是一个字符串,代表一个完整的 shell 命令,因此你可以包含参数、管道、<> I/O 重定向等:

>>> ret = subprocess.getoutput('date -u')
>>> ret
'Thu Jul 31 09:16:30 PM UTC 2025'

通过管道将输出字符串传送到wc 命令中,可以得到 1 行、7 个 "单词 "和 32 个字符:

>>> ret = subprocess.getoutput('date -u | wc')
>>> ret
'      1       7      32'

一个名为check_output() 的变体方法会接收一个命令和参数列表。默认情况下,它只以字节类型而非字符串形式返回标准输出,并且不使用 shell:

>>> ret = subprocess.check_output(['date', '-u'])
>>> ret
b'Thu Jul 31 09:18:00 PM UTC 2025\n'

要显示其他程序的退出状态,getstatusoutput() 会返回一个包含状态代码和输出的元组: ...

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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898