Python优化了从REST向数据库添加信息

时间:2018-09-10 07:13:58

标签: python-3.x sqlite

我编写了一个代码,使用REST通过提供任务密钥来获取任务信息, 问题是我需要从180,000,000扫描到999,999,999,而不知道存在哪些密钥,哪些不存在 该代码确实可以正常工作,但是非常慢...仅扫描了100,000,000 大约需要

我该如何采取不同的方式? 我尝试实现多处理但没有成功

这是我的代码:

db = sqlite3.connect('databse.db')
cursor = db.cursor()

for key_iterator in range(180021820,999999999):
    xml_result = REST_requests_test.getTaskInfo(str(key_iterator))

    fault_code = int((xml_result.find("faultcode")))
    if (fault_code != -1):
        print ("key " +str(key_iterator) + " Not Found")
        continue   

    jsn_info = json.dumps(xmltodict.parse(xml_result)) #Parse xml output from REST request to a JSON syntax
    jsn_info = cleanJson(jsn_info) #clean unneccecery stuff from JSON
    jsn_info.encode('utf-8')

    my_dict = json.loads(jsn_info) #create dictionary from json

    is_scheduled = int(my_dict["IsScheduled"]) #get task scheduling status

    if (is_scheduled):
        print (str(key_iterator)+" Task already schedule, Skipping")
        continue

    print ("Inserting "+str(key_iterator)+" to database")
    l = list(my_dict.values())
    str1 = [tuple(my_dict.values())]

    columns = ', '.join(my_dict.keys())
    placeholders = ', '.join('?' * len(my_dict))
    query = 'INSERT INTO Tasks (%s) VALUES ' % (columns)
    str2 = str(str1)
    str2 = str2[1:-1]
    query = query + " " + str2

    cursor.execute(query)
    db.commit()

0 个答案:

没有答案