首页 > 解决方案 > Python 多处理计数器更新

问题描述

我正在使用 python 多处理来调用一个名为“sql_fetch”的函数,该函数应该更新变量计数,它迭代我的列表(即“test_propid_entid”)的次数,以确定我从查询中获得良好数据的次数。这里 query_randomizer 如果我的函数调用生成查询并且我只想在多处理调用结束时在 count 变量中打印结果以确定 pandas 数据帧返回结果的次数(即查询返回记录的次数):我该如何实现?count 总是为我打印 1 因为它会重置我在下面在 multiprocessing 中使用的每个调用的值:

from tqdm import tqdm

start_dt = time()
multi =[]
with tqdm(total=len(test_propid_entid)) as pbar:
    for sub_prop_entid in test_propid_entid:
        t_sub = multiprocessing.Process(target=sql_fetch, args=(sub_prop_entid,))
        pbar.update()
        multi.append(t_sub)
        t_sub.start()
    for a in multi:
        a.join()
print('TOTAL TIME: ' ,time() - start_dt)

我想调用 sql_fetch 函数从 Oracle 查询引擎获取数据:

import pandas as pd
def sql_fetch(sub_prop_entid):
    count = 0 
    data= pd.read_sql(query_randomizer(
        sub_prop_entid[0], sub_prop_entid[1], arg1, arg2,), engine)
    num_records = len(pd.DataFrame(data).index)
    df = pd.DataFrame(data)
    if num_records > 0:
        count += 1
        print( "# Of Records............: " ,num_records , '\n')
        df.insert(0,'# Of Records',num_records)
        df.insert(1,'Exec Time',tot)
        display(df)
    print ("Records with good data", count)

标签: pythonmultiprocessingpython-multiprocessing

解决方案


您应该了解两者之间的区别。多处理和多线程。我相信后者是您完成任务的正确方法。

python 中的多处理允许您在多个“CPU”中并行运行任务,这意味着任何变量、数据或连接都不能被腌制(即共享)。将每个进程视为一个全新的 Python 程序——而您要做的是在不同的 Python 程序之间共享一个变量。

因此,尝试使用多线程并将计数器设置为全局变量。

import multithreading
global counter
for sub_prop_entid in test_propid_entid:
    t_sub = multithreading.Thread(target=sql_fetch, args=(sub_prop_entid,))
    multi.append(t_sub)
    t_sub.start()
for a in multi:
    a.join()

推荐阅读