Многопроцессорная обработка моего цикла / итерации (попробуйте ... кроме) - PullRequest
0 голосов
/ 19 июня 2019

Я преобразовываю одну химическую запись в другой тип. Мой список содержит более 6 тысяч различных имен для конвертации, и это занимает так много времени. Как я могу использовать многопроцессорность? Я пытался реализовать себя, но я нуб. Другие оптимизации кода также приветствуются!

Я пытался реализовать многопроцессорность сам, но я нуб.

def resolve(str_input, representation):
    import cirpy
    return cirpy.resolve(str_input, representation)

compound_list = []
smiles_list = []

for index, row in df_Verteilung.iterrows():

    try:
        actual_smiles = resolve(row['Compound'], 'smiles')

    except:
        actual_smiles = 'Error'

    print('\r', row['Compound'], actual_smiles, end='')

    compound_list.append(row['Compound'])
    smiles_list.append(actual_smiles)

df_new = pd.DataFrame({'Compound' : compound_list, 'SmilesCode' : smiles_list})
df_new.to_csv(index=False)

1 Ответ

0 голосов
/ 19 июня 2019

Попробуйте использовать пул из многопроцессорной системы:

    from multiprocessing import Pool

    def resolve(str_input, representation):
        try:
            import cirpy
            res =  cirpy.resolve(str_input, representation)
        except:
            res = "Error"

        print('\r', str_input, res, end='')
        return (str_input, res)

    n = 5

    with Pool(processes=n) as pool:
        compounds_smiles_list = pool.starmap(resolve, [(row['Compound'], 'smiles') for index, row in df_Verteilung.iterrows()])

    compound_list = [elem[0] for elem in compounds_smiles_list]
    smiles_list = [elem[1] for elem in compounds_smiles_list]

    df_new = pd.DataFrame({'Compound' : compound_list, 'SmilesCode' : smiles_list})
    df_new.to_csv(index=False)

Используя переменную n, вы управляете размером пула.Кроме того, вы можете оставить конструктор Pool пустым, и в соответствии с вашей системой будет выбрано оптимизированное количество рабочих.

Некоторые объяснения:

Пул

Starmap

...