2017-08-20 82 views
0

我用14个内核调用pool.apply_async()。pool.apply_async需要很长时间才能完成,如何加快速度?

import multiprocessing 
    from time import time 
    import timeit 

    informative_patients = informative_patients_2500_end[20:] 
    pool = multiprocessing.Pool(14) 
    results = [] 
    wLength = [20,30,50] 

    start = time() 

    for fn in informative_patients: 
     result = pool.apply_async(compute_features_test_set, args = (fn, 
     wLength), callback=results.append) 

    pool.close() 

    pool.join() 


    stop = timeit.default_timer() 

    print stop - start 

的问题是它完成调用用于第一13个数据compute_features_test_set()函数在不到一小时,但它需要一个多小时以完成最后一个。所有14个数据集的数据大小都是相同的。我试着在pool.close()之后放置pool.terminate(),但在这种情况下,它甚至不会启动池并立即终止池,而无需进入for循环。这总是以同样的方式发生,如果我使用更多的内核和更多的数据集,总是最后一个需要很长时间才能完成。我的compute_features_test_set()函数是一个简单的特征提取代码,并且可以正常工作。我在Linux red hat 6,python 2.7和jupyter上工作。计算时间对我来说很重要,我的问题是这里出了什么问题,以及我如何修复它以在合理的时间内完成所有计算?

+0

你有多少**真正的核心**? 'informative_parients'的大小有多大? – stovfl

+0

我想我在我工作的服务器上有32个真正的核心。 informative_parients只有14个患者ID,我在任何患者ID的for循环中称我的特征提取功能。因此,informative_parients是一个由14个字符串组成的数组,每个字符串的长度为8.我使用患者ID为每个患者收集信息,作为我的特征提取功能的输入。 – nightrain

回答

0

问题:......这里有什么问题,我该如何解决

无法赶上这是一个multiprocessing问题。
但是如何你得到这个:“总是最后一个需要这么长时间才能完成”
您正在使用callback=results.append而不是自己的function
编辑您的问题并显示如何timeit一个处理时间。
还可以将您的Python版本添加到您的问题。

执行以下操作来验证它不是一个数据问题

start = time() 
    results.append(
     compute_features_test_set(<First informative_patients>, wLength 
     ) 
    stop = timeit.default_timer() 
    print stop - start 

    start = time() 
    results.append(
     compute_features_test_set(<Last informative_patients>, wLength 
     ) 
    stop = timeit.default_timer() 
    print stop - start 

比较你得到的两倍。

相关问题