栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用Python发送100,000个HTTP请求的最快方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Python发送100,000个HTTP请求的最快方法是什么?

无捻解决方案:

from urlparse import urlparsefrom threading import Threadimport httplib, sysfrom Queue import Queueconcurrent = 200def doWork():    while True:        url = q.get()        status, url = getStatus(url)        doSomethingWithResult(status, url)        q.task_done()def getStatus(ourl):    try:        url = urlparse(ourl)        conn = httplib.HTTPConnection(url.netloc)conn.request("HEAD", url.path)        res = conn.getresponse()        return res.status, ourl    except:        return "error", ourldef doSomethingWithResult(status, url):    print status, urlq = Queue(concurrent * 2)for i in range(concurrent):    t = Thread(target=doWork)    t.daemon = True    t.start()try:    for url in open('urllist.txt'):        q.put(url.strip())    q.join()except KeyboardInterrupt:    sys.exit(1)

这比扭曲的解决方案要快一点,并且使用的CPU更少。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号