1

我正在尝试同时向服务器发送请求,然后使用以下代码记录平均延迟:

import Queue
import time
import threading
import urllib2

data = "{"image_1":"abc/xyz.jpg"}"
headers = {.....}
def get_url(q, url):
    num = 1
    sum = 0
    while num <= 200:
        start = time.time()
        req = urllib2.Request(url, data, headers)
        response = urllib2.urlopen(req)
        end = time.time()
        print end - start
        num = num + 1
        q.put(response.read())
        sum = sum + (end - start)
    print sum


theurls = ["http://example.com/example"]
q = Queue.Queue()

for u in theurls:
    t = threading.Thread(target = get_url, args = (q, u))
    t.daemon = True
    t.start()

while True:
    s = q.get()
    print s

这段代码工作得很好,但现在我打算每秒发送超过 1000 个请求。我遇到了这个答案,但我不确定如何grequests用于我的案例。一些见解将非常有帮助。

谢谢

4

1 回答 1

3

文档不是很好,但来源是。阅读源代码!查看grequests.pygithub 上的前几行:

"""
grequests
~~~~~~~~~
This module contains an asynchronous replica of ``requests.api``, powered
by gevent. All API methods return a ``Request`` instance (as opposed to
``Response``). A list of requests can be sent with ``map()``.
"""

该软件包导出以下内容

__all__ = (
    'map', 'imap',
    'get', 'options', 'head', 'post', 'put', 'patch', 'delete', 'request'
)

这些符号在文件的下方定义

# Shortcuts for creating AsyncRequest with appropriate HTTP method
get = partial(AsyncRequest, 'GET')
options = partial(AsyncRequest, 'OPTIONS')
head = partial(AsyncRequest, 'HEAD')
post = partial(AsyncRequest, 'POST')
put = partial(AsyncRequest, 'PUT')
patch = partial(AsyncRequest, 'PATCH')
delete = partial(AsyncRequest, 'DELETE')

partial是从functools文件顶部导入的。

from functools import partial

的文档functool.partial说:

返回一个新的部分对象,当调用它时,它的行为类似于使用位置参数 args 和关键字参数关键字调用的 func。如果为调用提供了更多参数,则将它们附加到 args。如果提供了额外的关键字参数,它们会扩展和覆盖关键字。

基本上打电话grequests.get来电AsyncRequest("GET", **other args go here**)AsyncRequest是一个创建新的函数AsyncRequest它的文档说:

""" Asynchronous request.
Accept same parameters as ``Session.request`` and some additional:
:param session: Session which will do request
:param callback: Callback called on response.
                 Same as passing ``hooks={'response': callback}``
"""

会话在前面定义:

from requests import Session

这是使用requests会话的指南。

于 2016-11-28T22:37:07.923 回答