Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
Python语言及其应用(第2版)
by
Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from
Python语言及其应用(第2版)
437
附录
C
截然不同的
async
前两个附录是写给程序员新手的,本附录则面向有经验的程序员。
和多数编程语言一样,
Python
也是
同步
的。
Python
以线性方式运行代码
,一次一行,从头
到尾。当你调用函数时,
Python
会转入函数代码
,调用方则一直等待,直到函数返回,然
后再恢复先前的操作。
CPU
同一时刻只
能做一件事,同步执行完全合情合理。但这样带来的结果是程序经常会什
么代码都不运行,一直等着文件数据或网络服务等。这就像我们在等着网页载入时盯着浏
览器屏幕傻看。如果能避免这种“忙等”,就能缩短程序的运行时长,提高
吞吐量
。
第
15
章讲过
可以使用线程、进程或者
gevent
或
twisted
等第三方解决方案实现并发。但
是现在
Python
和第三
方解决方案中内建了越来越多的
异步
方法。这些方法可以与普通的同
步
Python
代码共存,不过,借用捉鬼敢死队的一句警告:你不能穿过流束(
you can
’
t cross
the streams
)
。我将为你展示如何避免各种副作用。
C.1
协程和事件循环
Python 3.4
添加了标准异步模块
asyncio
,
Python 3.5
添加了关键字
async
和
await
。这实现
了一些新概念。
•
协程
,可以在任意点暂停的函数。
•
事件循环
,调度和运行协程。
由此编写的异步代码类似于普通的同步代码。否则,就得使用第
15
章和第
17
章介绍过以
及
C.3
节中总结的方法。
正常的多任务处理由操作系统实现。操作系统决定哪种方式公平,谁过多占用了
CPU
,何
438
|
附录
C
时执行
I/O
操作,等等。然而 ...
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.
Start your free trial
You might also like
Python编程入门与实战
Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南
Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程
保罗·J·戴特尔
Python面向对象编程指南
Posts & Telecom Press, Steven F. Lott
Publisher Resources
ISBN: 9787115586223