【问题标题】:Show progress over long sql execute command in python在 python 中显示长 sql 执行命令的进度
【发布时间】:2023-01-27 22:50:55
【问题描述】:

我创建了一个 python 脚本,它打开一个大型 SQL 文件(+50k 行),其中插入到表中。

代码运行良好,但需要几个小时,我想知道我是否可以显示进度条(tqdm 似乎不适用于这种情况)或只显示“通过时间”

代码:

def runScript(file):
    with open(file,'r') as f:
        sql = f.read()
    ...
    with conn.cursor() as cursor:
        c.execute(sql)  # --> this takes a lot of time

tqdm 不起作用(或至少不显示任何内容)。

我可以逐行阅读并使用 tqdm,但它需要更多时间。

任何想法表示赞赏。

【问题讨论】:

    标签: python tqdm


    【解决方案1】:

    您可以将进度打印到日志文件。每批行之后的当前时间,例如1000 行。这样您就可以了解已经过去了多少时间以及完成任务还剩多少时间。

    with conn.cursor() as cursor:
        for i in range(0, len(sql), 1000):
            c.execute(sql[i:i+1000])
            print(f"{time.ctime()} : {i} rows done")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多