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程式設計
196
|
第八章
grequests 爬蟲的呼叫時間線
時間
8-4 範例 8-5 HTTP 請求的時間線
tornado
Python 裡,另一個經常被使用的非同步 I/O 套件就是
tornado
,這最初是 Facebook
HTTP 客戶端與伺服器開發的套件。相對於
gevent
tornado
選擇使用回呼方法處理非
同步行為。不過,在 3.x 版本中,類 coroutine 的行為被增加進來,並與舊有的程式碼
兼容。
在範例 8-6 中,我們實作與先前的
gevent
相同的網路爬蟲,但使用
tornado
I/O 迴圈
(它的事件迴圈版本)與 HTTP 客戶端,幫我們免除必須將請求分批,以及處理程式碼
之較低階面向的其他麻煩。
範例
8-6 tornado
版本的
HTTP
爬蟲
from tornado import ioloop
from tornado.httpclient import AsyncHTTPClient
from tornado import gen
from functools import partial
import string
import random
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient",
max_clients=100) #
並行性
|
197
def generate_urls(base_url, num_urls):
for i in xrange(num_urls): ...
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