实现for循环的并行处理

时间:2016-12-09 18:10:26

标签: python python-3.x csv parallel-processing ipython-parallel

希望使以下代码并行 - 它以一种大的9gb专有格式读取数据,并根据30列数据生成30个单独的csv文件。目前在30分钟的数据集上写入每csv需要9分钟。 Python中并行库的解决方案空间有点压倒性。你能指导我学习任何好的教程/示例代码吗?我找不到任何有用的信息。

for i in range(0, NumColumns):
    aa = datetime.datetime.now()
    allData = [TimeStamp]
    ColumnData = allColumns[i].data    # Get the data within this one Column
    Samples = ColumnData.size           # Find the number of elements in Column data
    print('Formatting Column {0}'.format(i+1))
    truncColumnData = []                # Initialize truncColumnData array each time for loop runs       
    if ColumnScale[i+1] == 'Scale:  '+ tempScaleName:   # If it's temperature, format every value to 5 characters
        for j in range(Samples):
            truncValue = '{:.1f}'.format((ColumnData[j]))
            truncColumnData.append(truncValue)   # Appends formatted value to truncColumnData array

    allData.append(truncColumnData)   #append the formatted Column data to the all data array

    zipObject = zip(*allData)
    zipList = list(zipObject)

    csvFileColumn = 'Column_' + str('{0:02d}'.format(i+1)) + '.csv'    
    # Write the information to .csv file
    with open(csvFileColumn, 'wb') as csvFile:
        print('Writing to .csv file')
        writer = csv.writer(csvFile)
        counter = 0
        for z in zipList:
            counter = counter + 1
            timeString = '{:.26},'.format(z[0])
            zList = list(z)
            columnVals = zList[1:]
            columnValStrs = list(map(str, columnVals))
            formattedStr = ','.join(columnValStrs)
            csvFile.write(timeString + formattedStr + '\n')   # Writes the time stamps and channel data by columns

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案可能是使用Dask http://dask.pydata.org/en/latest/ 一位同事最近向我推荐了这个,这就是我想到的原因。