如何打印函数内部定义的字典



执行完这段代码后,我没有得到任何错误,但如何在多处理完成后打印Dictionary变量。df_Store是一个包含3列的数据帧——StoreID、Latitude、Longitude。这样做之后,我得到BrokenPipeError的错误:[Erno 32]Brokenpipe

df_Store = pd.read_parquet(r'C:UsersStore_Table .parquet',engine = 'auto', columns=None )
Lat_ary = df_Store['Latitude'].tolist()
Long_ary = df_Store['Longitude'].tolist()
col = list(zip(Lat_ary,Long_ary ))
df_Store['Lat_Long']= col
from haversine import haversine
import multiprocessing
import time
start = time.perf_counter()
def proximity_store(d, df_Store):
d={};
for i in range(len(df_Store)):
for j in range(len(df_Store)):
if df_Store.StoreID[i]==df_Store.StoreID[j]:
pass
else:
haversine(df_Store.Lat_Long[i], df_Store.Lat_Long[j])
d[df_Store.StoreID[i],df_Store.StoreID[j]] = haversine
return d
if __name__ == '__main__':
d = multiprocessing.Manager().dict()
p1 = multiprocessing.Process(target=proximity_store, args=[d, df_Store])
p1.start()
p1.join()

finish=time.perf_counter()
print(f'Finished in {round(finish-start,2)}second(s)')
print(d)

这里有一个解决方案,使用共享字典(multiprocessing.Manager().dict()(而不是标准的内置Python字典(请参阅另一个Stack Overflow问答,了解为什么不应该将内置字典传递给进程(。此外,我没有将Dictionary定义为全局变量,而是将其创建为局部变量,并将其作为参数传递给进程的目标函数(通常,尽可能避免全局变量是一种很好的做法(。

此外,为了演示的目的,我用随机生成的整数替换了未定义的变量,所以任何人都可以自己运行这个例子。

import time
import multiprocessing
import numpy as np
start = time.perf_counter()
def proximity_store(d):
np.random.seed(0)
len_df_Store = np.random.randint(5, 10)
for i in range(len_df_Store):
for j in range(len_df_Store):
df_Store_StoreID_i = np.random.randint(5, 10)
df_Store_StoreID_j = np.random.randint(5, 10)
if df_Store_StoreID_i==df_Store_StoreID_j:
pass
else:
haversine = np.random.randint(5, 10)
d[df_Store_StoreID_i, df_Store_StoreID_j] = haversine
return d
if __name__ == '__main__':
np.random.seed(0)
d = multiprocessing.Manager().dict()
p1 = multiprocessing.Process(target=proximity_store, args=[d])
p1.start()
p1.join()

finish=time.perf_counter()
print(f'Finished in {round(finish-start,2)}second(s)')
print(d)

控制台输出:

{(5, 8): 8, (8, 6): 6, (7, 9): 8, (5, 9): 5, (6, 5): 5, (9, 8): 8, (8, 5): 8, (5, 7): 8, (9, 5): 5, (6, 9): 7, (5, 6): 5, (7, 8): 8, (8, 9): 5, (7, 5): 6, (6, 8): 7, (9, 7): 5, (8, 7): 5, (7, 6): 7, (9, 6): 8, (6, 
7): 8}

相关内容

  • 没有找到相关文章

最新更新