Skip to Content
《高性能 Python》第二版
book

《高性能 Python》第二版

by Micha Gorelick, Ian Ozsvald
May 2025
Intermediate to advanced
468 pages
6h 20m
Chinese
O'Reilly Media, Inc.
Content preview from 《高性能 Python》第二版

第 9 章 多处理模块 多处理模块

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

CPython 默认不使用多 CPU。这一方面是因为 Python 是在单核时代设计的,另一方面是因为并行化实际上很难高效完成。Python 给我们提供了工具,但让我们自己做出选择。不过,看到你的多核机器在一个长期运行的进程中只使用一个 CPU 是很痛苦的,所以在本章中我们将回顾一下同时使用机器所有内核的方法。

备注

我们刚刚提到了CPython--我们都在使用的通用实现。Python 语言中没有任何规定阻止它使用多核系统。CPython 的实现无法高效地使用多核,但未来的实现可能不会受此限制。

我们生活在一个多核的世界--笔记本电脑普遍采用 4 核,台式机也有 32 核配置。如果您的工作可以拆分到多个 CPU 上运行,而不需要太多的工程设计工作,那么这是一个值得考虑的明智方向。

当 Python 用于在一组 CPU 上并行处理一个问题时,您可以期望在n 内核的情况下速度最多提高n 倍(n×)。如果您有一台四核机器,并且可以使用所有四个内核来完成任务,那么运行时间可能只有原来的四分之一。你不太可能看到超过 4 倍的速度提升;实际上,你可能会看到 3-4 倍的提升。

每增加一个进程都会增加通信开销,减少可用的 RAM,因此很少能实现n 倍的速度提升。 这类问题往往是任何并行编程的复杂之处,通常需要改变算法。 这就是为什么并行编程通常被认为是一门艺术。

如果您对Amdahl's law 不熟悉,不妨做一些背景阅读。该定律表明,如果只有一小部分代码可以并行化,那么无论使用多少 CPU 都没有用,整体运行速度仍然不会快很多。即使有很大一部分运行时间可以并行化,在达到收益递减点之前,能有效利用的 CPU 数量也是有限的。

multiprocessing 模块让你可以使用基于进程和线程的并行处理,通过队列共享工作,并在进程间共享数据。它主要侧重于单机多核并行(多机并行有更好的选择)。一个非常常见的用途是在一组进程中并行处理一个任务,以解决 CPU 受限的问题。您也可以使用OpenMP 来并行处理一个 I/O 绑定的问题,但正如我们在第 8 章所看到的,在这方面有更好的工具(例如,Python 3 中的新模块asynciotornado )。

备注

OpenMP 是多核的底层接口--您可能会想,是否应该关注它而不是multiprocessing 。我们在第 7 章介绍了它与 Cython 的关系,但本章不涉及它。multiprocessing 在更高层次上工作,共享 Python 数据结构,而 OpenMP 在编译成 C 语言后与 C 原始对象(如整数和浮点数)一起工作、只有在编译代码时,使用 OpenMP 才有意义;如果不编译 (例如,如果您使用高效的numpy 代码,并且希望在多个内核上运行),那么坚持使用multiprocessing 可能是正确的方法

要将任务并行化,你的思维方式必须与编写串行进程的常规方式稍有不同。你还必须接受这样的事实:调试并行化任务更加 ...

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

《学习 Python》第 5 版

《学习 Python》第 5 版

Mark Lutz
ppk on JavaScript

ppk on JavaScript

Peter-Paul Koch

Publisher Resources

ISBN: 9798341657946