Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
244
15
sh
delegator
pypeln
15.3
并发
官方
Python
网站基于标准库对并发做了一般性讨论,给出了各种包和技术的链接,本章将
展示其中最实用的部分。
在计算机中,如果你的程序在等待,那么通常是出于以下某个原因。
I/O 受限
这是最常见的原因。计算机
CPU
运行速度极快
,是内存的数百倍,是磁盘或网络的数
千倍。
CPU 受限
CPU
持续繁忙。这发生在
数字运算
任务中,比如科学计算或图形计算。
下面是两个与并发相关的术语。
同步
一件事跟着另一件事,就像一排小鹅跟在它们的父母后面一样。
异步
任务是独立的,就像小鹅随机跳入池塘中。
当你从简单的系统和任务转向现实问题时,迟早得处理并发。考虑一个网站。你通常必须
We
b
客户快速地返回静态网页和动态网页。几分之一秒的时间还可以被认为是具有互动
性,但如果时间再长一点儿,人们就会变得不耐烦。谷歌和亚马逊等公司的测试表明,如
果页面的加载速度哪怕慢了一点儿,流量就会迅速下滑。
但如果你对那些耗时的任务(比如上传文件、调整图片大小或查询数据库)又无能为力,
该怎么办呢?在并发的
We
b
服务器代码中是无法解决的,因为同步必然会产生等待。
在单台计算机中,要想尽可能快地执行多个任务,就得让任务之间独立。缓慢的任务不应
该阻塞其他任务。
本章前面展示过
multiprocessing
如何用于单机并发任务。如果需要调整图片大小,则
Web
服务器代码应该调用一个独立的
、专门的图片缩放进程,以异步方式并发运行。它通
过调用多个缩放过程来水平扩展应用。
诀窍在于让它们彼此协同工作。任何共享的控制或状态都将意味着瓶颈。一个更大的诀窍 ...
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

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
量子计算机编程:从入门到实践

量子计算机编程:从入门到实践

Eric R. Johnston, Nicholas Harrigan, Mercedes Gimeno-Segovia
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787115586223