Pythonのrequests ライブラリのドキュメント内で提供されているサンプルを試しました。
でasync.map(rs)
、レスポンスコードを取得しますが、リクエストされた各ページのコンテンツを取得したいです。たとえば、これは機能しません。
out = async.map(rs)
print out[0].content
Pythonのrequests ライブラリのドキュメント内で提供されているサンプルを試しました。
でasync.map(rs)
、レスポンスコードを取得しますが、リクエストされた各ページのコンテンツを取得したいです。たとえば、これは機能しません。
out = async.map(rs)
print out[0].content
以下の回答は、 v0.13.0 以降のリクエストには適用されません。この質問が書かれた後、非同期機能はgrequestsに移動されました。ただし、以下に置き換えるだけrequests
でgrequests
機能するはずです。
v0.13.0未満のリクエストの使用に関する元の質問を反映するために、この回答をそのまま残しました。
async.map
非同期で複数のタスクを実行するには、次のことを行う必要があります。
async.map
すべてのリクエスト / アクションのリストを呼び出す例:
from requests import async
# If using requests > v0.13.0, use
# from grequests import async
urls = [
'http://python-requests.org',
'http://httpbin.org',
'http://python-guide.org',
'http://kennethreitz.com'
]
# A simple task to do to each response object
def do_something(response):
print response.url
# A list to hold our things to do via async
async_list = []
for u in urls:
# The "hooks = {..." part is where you define what you want to do
#
# Note the lack of parentheses following do_something, this is
# because the response will be used as the first argument automatically
action_item = async.get(u, hooks = {'response' : do_something})
# Add the task to our list of things to do via async
async_list.append(action_item)
# Do our list of things to do via async
async.map(async_list)
async
は独立したモジュールになりました: grequests
.
ここを参照してください: https://github.com/kennethreitz/grequests
そして、そこに: Python 経由で複数の HTTP リクエストを送信するための理想的な方法は?
$ pip install grequests
スタックを構築します。
import grequests
urls = [
'http://www.heroku.com',
'http://tablib.org',
'http://httpbin.org',
'http://python-requests.org',
'http://kennethreitz.com'
]
rs = (grequests.get(u) for u in urls)
スタックを送る
grequests.map(rs)
結果は次のようになります
[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]
grequests は、同時リクエスト、つまり複数のリクエストが同じサーバーに送信される場合に制限を設定していないようです。
これがしばらく閉鎖されていたことは知っていますが、リクエスト ライブラリに基づいて構築された別の非同期ソリューションを促進するのに役立つかもしれないと考えました。
list_of_requests = ['http://moop.com', 'http://doop.com', ...]
from simple_requests import Requests
for response in Requests().swarm(list_of_requests):
print response.content
ドキュメントはこちら: http://pythonhosted.org/simple-requests/
from threading import Thread
threads=list()
for requestURI in requests:
t = Thread(target=self.openURL, args=(requestURI,))
t.start()
threads.append(t)
for thread in threads:
thread.join()
...
def openURL(self, requestURI):
o = urllib2.urlopen(requestURI, timeout = 600)
o...
私はしばらくの間、github の gist API に対する非同期呼び出しに Python リクエストを使用してきました。
例については、次のコードを参照してください。
https://github.com/davidthewatson/flasgist/blob/master/views.py#L60-72
このスタイルの python は最も明確な例ではないかもしれませんが、コードが機能することは保証できます。これがあなたを混乱させるかどうか教えてください。私はそれを文書化します。
I have also tried some things using the asynchronous methods in python, how ever I have had much better luck using twisted for asynchronous programming. It has fewer problems and is well documented. Here is a link of something simmilar to what you are trying in twisted.
http://pythonquirks.blogspot.com/2011/04/twisted-asynchronous-http-request.html