0

我正在尝试按照purchase_state以下步骤从谷歌播放中提取:

    import base64
    import requests
    import smtplib
    from collections import OrderedDict
    import mysql.connector
    from mysql.connector import errorcode
    ......
  1. 查询数据库,从我的表中返回数千行purchase_id字段
  2. 检查 db 中的每一行,并提取 purchase_id,然后查询所有这些行的 google play。例如,如果我上一步的结果是 1000,则 1000 次是查询 google(刷新令牌 + 查询)。
  3. purchase status除了从 mysql 查询中获取的其他一些字段之外,将 google play 中的新字段添加到新字典中。
  4. 最后一步是对我的 dic 进行循环,然后准备所需的报告

编辑后:

def build_dic_from_db(data,access_token):
dic = {}
for row in data:

    product_id = row['product_id']
    purchase_id = row['purchase_id']
    status = check_purchase_status(access_token, product_id,purchase_id)
    cnt = 1

    if row['user'] not in dic:
        dic[row['user']] = {'id':row['user_id'],'country': row['country_name'],'reg_ts': row['user_registration_timestamp'],'last_active_ts': row['user_last_active_action_timestamp'],
                            'total_credits': row['user_credits'],'total_call_sec_this_month': row['outgoing_call_seconds_this_month'],'user_status':row['user_status'],'mobile':row['user_mobile_phone_number_num'],'plus':row['user_assigned_msisdn_num'],
                            row['product_id']:{'tAttemp': cnt,'tCancel': status}}
    else:
        if row['product_id'] not in dic[row['user']]:
            dic[row['user']][row['product_id']] = {'tAttemp': cnt,'tCancel':status}
        else:
            dic[row['user']][row['product_id']]['tCancel'] += status
            dic[row['user']][row['product_id']]['tAttemp'] += cnt
return dic

问题是我的代码运行缓慢〜总执行时间:448.7483880519867,我想知道是否有改进我的脚本。有什么建议吗?

4

1 回答 1

1

我希望我是对的,但瓶颈似乎是与 Playstore 的连接。按顺序执行将需要很长时间,而服务器一次能够处理一百万个请求。因此,这是一种使用执行程序处理作业的方法(您需要安装“并发”包)。在该示例中,您将能够同时发送 100 个请求。

from concurrent import futures
EXECUTORS = futures.ThreadPoolExecutor(max_workers=100)
jobs = dict()
for row in data:

   product_id = row['product_id']
   purchase_id = row['purchase_id']
   job = EXECUTORS.submit(check_purchase_status,
access_token, product_id,purchase_id)
   jobs[job] = row

for job in futures.as_completed(jobs.keys()):
   # here collect your results and do something useful with them :)
   status = job.result()
   # make the connection with current row
   row = jobs[job]
   # now you have your status and the row

顺便说一句,尝试使用临时变量,或者您不断地使用相同的键访问您的字典,这不利于代码的性能和可读性。

于 2016-06-17T10:52:08.183 回答