Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
Python 中的執行緒
|
445
Python 中的執行緒
Python 在支援執行緒的平台(例如 Win32Linux 和其他的 Unix 變體)上
提供多執行緒功能。如果一個動作在開始到結束之間保證不會發生執行緒
的切換(thread switching),那個動作就被稱為是
不可分割
atomic
,或稱
「原子式」)的。實務上,在 CPython 中,在內建型別上執行時
看似
不可
分割的運算(例如簡單的指定或存取)大多
不可分割的(然而,擴增指
定和多重指定不是不可分割的)。但是,在多數情況下,仰賴不可分割性
atomicity
不是
一個好主意。你所處理的可能是使用者所編寫的類別之
實體,而非內建型別的,所以其中可能有對 Python 程式碼的隱含呼叫,使
得不可分割性的假設不一定成立。仰賴取決於實作的不可分割性可能會讓
你的程式碼被綁定到特定的實作,妨礙未來的更新。你最好使用涵蓋在本
章剩餘部分中的同步機能(synchronization facilities),而非仰賴不可分割
性的假設。
Python 提供兩種多執行緒功能。一個較舊且較低階的模組
_thread
(在 v2
中名為
thread
),具有不建議直接用在你程式碼中的低階功能性,我們在
本書中並不涵蓋
_thread
。較高階的模組
threading
奠基於
_thread
之上,
是推薦使用的那一個。多執行緒系統中關鍵的議題是如何以最佳的方式協
調多個執行緒。
threading
提供數種同步物件(synchronization objects)。
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

測試驅動開發|使用 Python

Harry J.W. Percival

Publisher Resources

ISBN: 9789864766819