187

Pythonのrequests ライブラリのドキュメント内で提供されているサンプルを試しました。

async.map(rs)、レスポンスコードを取得しますが、リクエストされた各ページのコンテンツを取得したいです。たとえば、これは機能しません。

out = async.map(rs)
print out[0].content
4

14 に答える 14

180

ノート

以下の回答は、 v0.13.0 以降のリクエストには適用されません。この質問が書かれた後、非同期機能はgrequestsに移動されました。ただし、以下に置き換えるだけrequestsgrequests機能するはずです。

v0.13.0未満のリクエストの使用に関する元の質問を反映するために、この回答をそのまま残しました。


async.map 非同期で複数のタスクを実行するには、次のことを行う必要があります。

  1. 各オブジェクト (タスク) で何をしたいかの関数を定義します。
  2. その関数をリクエストにイベントフックとして追加します
  3. 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)
于 2012-02-08T07:23:17.847 に答える
93

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 は、同時リクエスト、つまり複数のリクエストが同じサーバーに送信される場合に制限を設定していないようです。

于 2012-08-14T09:47:09.743 に答える
8

これがしばらく閉鎖されていたことは知っていますが、リクエスト ライブラリに基づいて構築された別の非同期ソリューションを促進するのに役立つかもしれないと考えました。

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/

于 2013-10-21T14:57:30.257 に答える
3
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...
于 2013-01-16T23:32:54.210 に答える
2

私はしばらくの間、github の gist API に対する非同期呼び出しに Python リクエストを使用してきました。

例については、次のコードを参照してください。

https://github.com/davidthewatson/flasgist/blob/master/views.py#L60-72

このスタイルの python は最も明確な例ではないかもしれませんが、コードが機能することは保証できます。これがあなたを混乱させるかどうか教えてください。私はそれを文書化します。

于 2012-02-23T05:35:36.327 に答える
1

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

于 2012-02-02T17:06:14.183 に答える