Skip to Content
流畅的Python(第2版)
book

流畅的Python(第2版)

by Luciano Ramalho
April 2023
Intermediate to advanced
769 pages
25h 16m
Chinese
Posts & Telecom Press
Content preview from 流畅的Python(第2版)
552
19
50 000
次迭代(因为
range
的步幅是
2
)休眠一次。
asyncio
仓库中的
284
号工单详细讨论了
asyncio.sleep(0)
的用法。
然而,需要注意的是,这将拖慢
is_prime
,以及事件循环和整个程序的速度——后二
者才是重点。换成每
10
万次迭代执行一次
await asyncio.sleep(0)
之后,在我的设备
中,旋转指针依然顺滑,只是用时达到了
4.9
秒,比原来的
primes.is_prime
函数慢了
将近
50%
(参数同为
5_000_111_000_222_021
)。
使用
await asyncio.sleep(0)
只是权宜之计,更稳妥的办法是重构异步代码,把
CPU
密集型计算委托给另一个进程。第
21
章将给出一个使用
asyncio.loop.run_in_
executor
的方案。另外,还可以使用任务队列(详见
19.7.5
)。
目前,我们所做的实验只调用了一个
CPU
密集型函数。
19.6
节说明如何并发执行多个
CPU
密集型调用。
19.6
 自建进程池
我写这一节是为了展示如何使用多个进程处理
CPU
密集型任务,以及使用
队列分配任务和收集结果的常见模式。第
20
章将给出一种为进程分配任务
更简单的方式:
concurrent.futures
包中的
ProcessPoolExecutor
(内部使用
队列)。
本节将编写一个程序,计算
2
9 999 999 999 999 999
10
16
1
)之间或大于
2
53
20
个整
数样本是否为素数。样本中的素数有大有小,还有可以分解为大小素数的合数。 ...
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高级编程(第2版)

Python高级编程(第2版)

Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
Python贝叶斯分析(第2版)

Python贝叶斯分析(第2版)

Posts & Telecom Press, Osvaldo Martin
高效能PYTHON程式設計

高效能PYTHON程式設計

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9787115612366