Python Mutiprocessing.pool 传递元组行

Python Mutiprocessing.pool passing row of touple

我在下面有一个代码,可以调用 mysql 来获取我需要请求的报告列表。然后它调用另一个函数 "call_report" 来请求报告。 call_report 函数在 CPU 中很低,主要是等待返回报告。我想同时 运行 for 循环的多个实例,但无法弄清楚如何在迭代 reports_to_run 中的行的地方实现池。

cursor.execute("SELECT * FROM tbl_rpt_log")
reports_to_run = cursor.fetchall() 

for row in reports_to_run :
    user=(row[0])
    report=(row[1])
    run_interval=(row[3])

   call_report(report, user)    

使用 Pool 的一般技巧是创建一个包装函数,它接受输入,将其转换为实际函数所需的输入,然后调用它。像这样:

def report_on_row(row):
    user = row[0]
    report = row[1]
    run_internal= row[3]
    return call_report(report, user)

现在,您只需使用 map 在每一行上调用此包装函数:

pool.map(report_on_row, reports_to_run)