Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过grequests调用函数_Python_Asynchronous_Request_Grequests - Fatal编程技术网

Python 通过grequests调用函数

Python 通过grequests调用函数,python,asynchronous,request,grequests,Python,Asynchronous,Request,Grequests,我知道在grequests上有很多帖子,比如 它描述了grequests的基本用法,以及如何通过grequests.get()发送钩子。我从链接中提取了这段代码 import grequests urls = [ 'http://python-requests.org', 'http://httpbin.org', 'http://python-guide.org', 'http://kennethreitz.com' ] # A simple task to

我知道在grequests上有很多帖子,比如 它描述了grequests的基本用法,以及如何通过
grequests.get()
发送钩子。我从链接中提取了这段代码

import grequests

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 ('print_test')

# A list to hold our things to do via async
async_list = []

for u in urls:
    action_item = grequests.get(u, hooks = {'response' : do_something})

    async_list.append(action_item)

# Do our list of things to do via async
grequests.map(async_list)
然而,当我运行这个程序时,我没有得到任何输出

/$ python test.py
/$
因为有4个链接,我希望输出是

print_test
print_test
print_test
print_test

我一直在四处搜索,没有找到缺少输出的原因。有趣的是,我遗漏了一些关键信息。

我还需要检查源代码,但是如果您将钩子函数重写为

# A simple task to do to each response object
def do_something(response, *args, **kwargs):
    print ('print_test')

它将输出。所以它可能无法调用原始钩子(因为它传递的参数比您接受的多)并捕获异常,所以您没有得到任何输出,所以我需要检查源代码,但如果您将钩子函数重写为

/$ python test.py
/$
# A simple task to do to each response object
def do_something(response, *args, **kwargs):
    print ('print_test')

它将输出。因此,它可能无法调用您的原始钩子(因为它传递的参数比您接受的要多)和捕获异常,因此您没有得到任何输出

非常感谢您的响应,只是尝试了一下,效果非常好!我相信你关于争论的数量是正确的。原来的库将所有关键字参数传递给钩子函数<代码>{'proxies':OrderedDict(),'timeout':None,'stream':False,'cert':None,'verify':True}非常感谢您的响应,刚刚尝试了它,效果非常好!我相信你关于争论的数量是正确的。原来的库将所有关键字参数传递给钩子函数<代码>{'proxies':OrderedDict(),'timeout':无,'stream':False,'cert':无,'verify':True}
/$ python test.py
/$