Asynchronous Requests with Python requests

前端 未结 12 1466
予麋鹿
予麋鹿 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 08:59

    maybe requests-futures is another choice.

    from requests_futures.sessions import FuturesSession
    
    session = FuturesSession()
    # first request is started in background
    future_one = session.get('http://httpbin.org/get')
    # second requests is started immediately
    future_two = session.get('http://httpbin.org/get?foo=bar')
    # wait for the first request to complete, if it hasn't already
    response_one = future_one.result()
    print('response one status: {0}'.format(response_one.status_code))
    print(response_one.content)
    # wait for the second request to complete, if it hasn't already
    response_two = future_two.result()
    print('response two status: {0}'.format(response_two.status_code))
    print(response_two.content)
    

    It is also recommended in the office document. If you don't want involve gevent, it's a good one.

提交回复
热议问题