【问题标题】:Multiprocessing with complex function具有复杂功能的多处理
【发布时间】:2018-02-15 09:23:57
【问题描述】:

我想在 python 中为一个相当复杂的函数创建一个多进程: 我已经用这样一个不太复杂的代码测试了这个函数:

from joblib import Parallel, delayed, parallel_backend
from joblib import load, dump

def print_hello(hallo, tschüß, rechnen,i):
    print(i)
    print(hallo[2])
    print (tschüß)
    rechnen = rechnen +i
    hallo2 = pd.DataFrame(hallo)
    hallo2.to_csv('./hallo'+str(i)+'.csv')
    hallo1 = pd.read_csv('./hallo'+str(i)+'.csv')
    return rechnen

hallo = ['hallo', 'hi', 'hey']
tschüß = 'tschüß'    
with parallel_backend('threading'):
test = Parallel()(delayed(print_hello)(hallo, tschüß, rechnen, i) for i in range(10))

print(test)

这很好用。但是我得到以下错误代码:

joblib.my_exceptions.TransportableException: TransportableException

...

joblib.my_exceptions.JoblibTypeError: JoblibTypeError

...

TypeError: sum_row() 缺少 1 个必需的位置参数:'i'

当我想让我的复杂函数工作时,看起来像这样:

def sum_row(count_series, path, folder, files_1, files_2, files_3, path_raw, i):
    print(i)
    df1 = pd.read_csv(path_raw + files_1[i], sep=',', low_memory=False)
    df2 = pd.read_csv(path_raw + files_2[i], sep=',', low_memory=False)
    df3 = pd.read_csv(path_raw + files_3[i], sep=',', low_memory=False)

    ##do some operations with those files and create df_test

    df_test.to_csv(path + folder + files_export[i])

    return 0

with parallel_backend('threading'):
    test = Parallel()(delayed(sum_row)(count_series, path, files_1, files_2, files_3,  path_raw, i) for i in range(len(files_1)))

【问题讨论】:

  • 您在调用函数时缺少文件夹参数。
  • 哦不...这样一个基本错误,成功了,谢谢。
  • 没问题很高兴帮助:)
  • @mgracer 你知道为什么它很慢而且只使用了一小部分 CPU 资源吗?它使用了许多 CPU,但都只是一点点?
  • 用调试工具搞清楚有python模块可以调试性能

标签: python multithreading parallel-processing multiprocessing joblib


【解决方案1】:

您收到错误的原因是您在调用函数时缺少文件夹参数。

test = Parallel()(delayed(sum_row)(count_series, path, folder, files_1, files_2, 
files_3,  path_raw, i) for i in range(len(files_1)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多