我只是想发送请求,但不想浪费时间等待响应。因为这些回复对我来说毫无用处。 我抬头看python文档,但没有找到解决方案。 感谢您的任何建议。 我试图使用 urllib2.urlopen(url, timeout=0.02)
但我不能确定请求是否实际发出。使用urllib2.urlopen()加载一个URL而不等待回复
1
A
回答
4
这被称为异步加载,这里是一个blog post explaining how to do it with 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而不等待响应。
0
实现异步函数调用的最简单方法是在其自己的线程或子进程中使用可调用函数。
http://docs.python.org/library/threading.html#threading.Thread
http://docs.python.org/library/multiprocessing.html#multiprocessing.Process
似乎有HTTP库用于Python已经实现然而这样的功能,如grequests
或requests-futures
(二者均基于所述requests
库,在超过urllib2
的改进API术语)。
相关问题
- 1. 的Python urllib2.urlopen在URL
- 2. 使用时urllib2.urlopen()打开一个URL“得到”或“后”
- 3. 等待加载URL返回数据AS3
- 4. 复制一个项目而不等待
- 5. 等待页面使用urllib2加载资源
- 6. 发送curl而不等待回复php
- 7. urllib2.urlopen()返回不同的结果
- 8. “urllib2.urlopen”添加主机头
- 9. python urllib2.urlopen返回错误500,Chrome浏览器加载页面
- 10. urllib2.urlopen不添加 “/” 最后的URL与中国自动
- 11. python:不能使用urlopen!从urllib,urllib2,clientcookie urlopen ssl错误
- 12. urllib.retrieve和urllib2.urlopen在下载图像后永不返回
- 13. 使用urllib2.urlopen下载.rar文件时缺少第一行()
- 14. urllib.urlopen工程,但urllib2.urlopen不
- 15. 的urllib2的urlopen HTML不全
- 16. 蟒蛇urllib2.urlopen(URL)过程块
- 17. 如何在url中处理urllib2.urlopen?
- 18. urllib2.urlopen失败,而urllib.urlopen工作在相同的URL
- 19. GetChar不等待回复
- 20. 等待一个进程的回复Erlang
- 21. 404错误urllib2.urlopen()
- 22. 无缓冲urllib2.urlopen
- 23. Python:用SIGTERM打断urllib2.urlopen()
- 24. urllib2.urlopen()在GAE上返回特定URL上的错误500
- 25. urllib2.urlopen返回一个没有info()或geturl()方法的对象
- 26. 等待加载
- 27. 等待加载
- 28. 找插座urllib2.urlopen返回值HTTP
- 29. PHP - 等待回复
- 30. 等待AJAX回复