Asynchronous Requests with Python requests

前端 未结 12 1299
予麋鹿
予麋鹿 2020-11-22 08:47

I tried the sample provided within the documentation of the requests library for python.

With async.map(rs), I get the response codes, but I want to get

相关标签:
12条回答
  • 2020-11-22 09:05

    async is now an independent module : grequests.

    See here : https://github.com/kennethreitz/grequests

    And there: Ideal method for sending multiple HTTP requests over Python?

    installation:

    $ pip install grequests
    

    usage:

    build a stack:

    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)
    

    send the stack

    grequests.map(rs)
    

    result looks like

    [<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]
    

    grequests don't seem to set a limitation for concurrent requests, ie when multiple requests are sent to the same server.

    0 讨论(0)
  • 2020-11-22 09:12
    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...
    
    0 讨论(0)
  • 2020-11-22 09:13

    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

    0 讨论(0)
  • 2020-11-22 09:15

    I have been using python requests for async calls against github's gist API for some time.

    For an example, see the code here:

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

    This style of python may not be the clearest example, but I can assure you that the code works. Let me know if this is confusing to you and I will document it.

    0 讨论(0)
  • 2020-11-22 09:17

    You can use httpx for that.

    import httpx
    
    async def get_async(url):
        async with httpx.AsyncClient() as client:
            return await client.get(url)
    
    urls = ["http://google.com", "http://wikipedia.org"]
    
    # Note that you need an async context to use `await`.
    await asyncio.gather(*map(get_async, urls))
    

    if you want a functional syntax, the gamla lib wraps this into get_async.

    Then you can do

    
    await gamla.map(gamla.get_async(10))(["http://google.com", "http://wikipedia.org"])
    

    The 10 is the timeout in seconds.

    (disclaimer: I am its author)

    0 讨论(0)
  • 2020-11-22 09:20

    If you want to use asyncio, then requests-async provides async/await functionality for requests - https://github.com/encode/requests-async

    0 讨论(0)
提交回复
热议问题