2

我正在做一个程序。它从文件中读取 url,然后发出请求。但是处理程序不会被调用。

这是我的代码:

import grequests


def main():
    async_list =[]

    for site in extractsites():
        action_item = grequests.get('http://' + site, hooks={'response': handleresponse})
        async_list.append(action_item)

    grequests.map(async_list)


def extractsites():
    return ['google.com']


def handleresponse(response):
    print(response)


if __name__ == '__main__':
    main()

谁能明白为什么它不起作用?

4

1 回答 1

2

处理程序必须具有不同的签名,可以传递其他参数:

def handler(response, **kwargs):
    print(response, kwargs)

完整示例:

import grequests

def main():
    async_list =[]

    for site in extractsites():
        action_item = grequests.get('http://' + site, hooks={'response': handleresponse})
        async_list.append(action_item)

    grequests.map(async_list)

def extractsites():
    return ['google.com']

def handleresponse(response, **kwargs):
    print(response, kwargs)

if __name__ == '__main__':
    main()

或更短:

import grequests
urls = ['http://google.com']
handler = lambda response, **kwargs: print(response, kwargs)
grequests.map(grequests.get(url, hooks={'response': handler}) for url in urls)
于 2017-08-26T14:27:54.440 回答