使用GRequests

时间:2017-08-26 14:01:25

标签: python http asynchronous python-requests grequests

我正在制作一个节目。它从文件中读取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()

有人能看出为什么它不起作用吗?

1 个答案:

答案 0 :(得分:1)

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

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)