Skip to Main Content
高效能PYTHON程式設計
book

高效能PYTHON程式設計

by Micha Gorelick, Ian Ozsvald
August 2015
Intermediate to advanced content levelIntermediate to advanced
384 pages
7h 42m
Chinese
GoTop Information, Inc.
Content preview from 高效能PYTHON程式設計
200
|
第八章
tornado 爬蟲的呼叫時間線
時間
8-5 範例 8-6 HTTP 請求的時間線
AsyncIO
由於使用非同步功能處理 I/O 密集系統的做法越來越普遍,Python 3.4+ 改寫了舊有的
asyncio
標準程式庫模組,這個模組從
gevent
tornado
的並行方法汲取了它的大部分影
響力,在那裡,coroutine 被定義並且被 yield from,以便暫停當前函式的執行,讓其他
coroutine 運作。就像在
tornado
裡,事件迴圈明確地被啟動,好讓 coroutine 開始執行。
此外,Python 3 引進了新的關鍵字
yield
from
〔譯註〕
,大幅簡化這些 coroutine 的處理(不
必再為了從 coroutine 回傳值而丟出例外,如範例 8-6 所做的那樣)。
注意,
asyncio
程式庫是非常低階的,它並未提供使用者較高階的功能性。例如,雖然
有非常完整的 socket API,但並沒有簡單的方法可以進行 HTTP 請求。因此,我們在範
8-8 中選擇使用
aiohttp
程式庫。無論如何,
asyncio
程式庫的採用剛剛開始增溫,其
輔助模組的世界可能會有一番劇烈的演變。
〔譯註〕 請參考
https://docs.python.org/3/whatsnew/3.3.html
https://www.python.org/dev/peps/pep-0380/
並行性
|
201
範例
8-8 asyncio
版本的
HTTP
爬蟲
import asyncio ...
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

流畅的Python

Luciano Ramalho
手把手教会你linux

手把手教会你linux

桑德.范.乌格特

Publisher Resources

ISBN: 9789863477105