Python - 1000-2000 API请求

时间:2015-04-26 15:12:54

标签: python mysql performance api

我正在尝试向API发出大量GET请求(1000到2000之间)。到目前为止,它需要超过5分钟,MySQL服务器只关闭我的连接。

我想在不到一分钟的时间内完成它。应该可以吗?

这是我到目前为止所做的:

def get_data(devices):

        for dev in devices: #array containing around 1000 devices
            model = str(dev[0])
            brand = str(dev[1])    
            model = model.replace(" ", "%20")
            brand = brand.replace(" ","%20")

            os = urllib2.urlopen('https://api.com/getData?&brand=' + brand + '&model='+ model).read()
            xmldoc = minidom.parseString(os)

            for element in xmldoc.getElementsByTagName('name'):
                print (element.firstChild.nodeValue)

1 个答案:

答案 0 :(得分:0)

您编码的瓶颈可能是网络I / O,如果您想更快地执行此操作,可以尝试使用gevent 库。

我用它做了很多IP地址跟踪路由,它比多线程更快。 Here是一些帮助您入门的演示代码。