首页 > 解决方案 > 在 Python 中并行运行脚本

问题描述

我有一个一直在创建文件的文件夹,一旦文件大小达到 100 兆字节,就意味着它已准备好加载到数据库中。加载过程最终应该并行完成。同时五个文件。

在 Powershell 中,我通过调用并行命令轻松地做到了这一点。

我编写了一个脚本,打开与数据库的连接,运行程序并执行BULK INSERT将文件上传到数据库的命令。

import pymssql
import os

try:
    # Open DB Connection
    conn = pymssql.connect(server=server, database=Database, timeout=60)
    c1 = conn.cursor(as_dict=True)
    #Run First procedure with the File Name Variable
    c1.callproc("First.procedure", (FileName ,))
    # Recieve Variables from the First Procedure
    for row in c1:
        NewFileId= row['FileId']
        DestinationTableName= row['DestinationTableName']
    conn.commit()
    # Upload the CSV file to the DB
        string = "BULK INSERT DestinationTableName FROM '{CSV FILE}' WITH( FIELDTERMINATOR = ',', DATAFILETYPE = 'CHAR');"
        c1.execute(string)
        conn.commit()
    #Run the Second procedure with the File ID and Table Name Variables
        c1.callproc("2nd.procedure", (FileId, DestinationTableName,))
        conn.commit()
    conn.close()
    f.close()

except pymssql.Error as e:
    File = open("Error.txt", "a")
    File.write(repr(e))

我想通过一次发送五个文件名来并行运行脚本。通过 Python 实现的任何建议?

标签: python

解决方案


推荐阅读