1

I just want to send the request but don't want to waste time waiting for the responses. Because the responses are useless to me. I looked up the python doc but I didn't find a solution. Thanks for any advice. I have tried to use urllib2.urlopen(url, timeout=0.02) But I can't sure if the request is actually sent out.

4

2 回答 2

4

这称为异步加载,这里有一篇博客文章解释了如何使用 urllib2 来实现。示例代码:

#!/usr/bin/env python

import urllib2
import threading

class MyHandler(urllib2.HTTPHandler):
    def http_response(self, req, response):
        print "url: %s" % (response.geturl(),)
        print "info: %s" % (response.info(),)
        for l in response:
            print l
        return response

o = urllib2.build_opener(MyHandler())
t = threading.Thread(target=o.open, args=('http://www.google.com/',))
t.start()
print "I'm asynchronous!"

这将加载 URL 而无需等待响应。

于 2013-07-25T16:24:05.390 回答
0

实现这样的异步函数调用的最简单方法是在其自己的线程或子进程中使用可调用对象。

http://docs.python.org/library/threading.html#threading.Thread
http://docs.python.org/library/multiprocessing.html#multiprocessing.Process

但是,似乎有一些用于 Python 的 HTTP 库已经实现了此类功能,例如grequestsor requests-futures(两者都基于该requests库,在 API 方面有所改进urllib2)。

于 2013-07-25T16:26:41.780 回答