Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将多个文件读入多个线程/进程以优化数据分析?_Python_Multithreading_Multiprocessing_Python Multiprocessing_Python Multithreading - Fatal编程技术网

Python 如何将多个文件读入多个线程/进程以优化数据分析?

Python 如何将多个文件读入多个线程/进程以优化数据分析?,python,multithreading,multiprocessing,python-multiprocessing,python-multithreading,Python,Multithreading,Multiprocessing,Python Multiprocessing,Python Multithreading,我试图用python读取3个不同的文件,并从中提取数据。然后我想把数据合并成一个大文件 由于每个单独的文件都已经很大,并且需要一些时间来进行数据处理,我想如果 我可以一次读取所有三个文件(在多线程/进程中) 等待所有文件完成该过程 当所有输出就绪后,将所有数据通过管道传输到下游函数以合并它 是否有人可以建议对此代码进行一些改进,以满足我的需要 import pandas as pd file01_output = ‘’ file02_output = ‘’ file03_output =

我试图用python读取3个不同的文件,并从中提取数据。然后我想把数据合并成一个大文件

由于每个单独的文件都已经很大,并且需要一些时间来进行数据处理,我想如果

  • 我可以一次读取所有三个文件(在多线程/进程中)
  • 等待所有文件完成该过程
  • 当所有输出就绪后,将所有数据通过管道传输到下游函数以合并它
是否有人可以建议对此代码进行一些改进,以满足我的需要

import pandas as pd

file01_output = ‘’
file02_output = ‘’
file03_output = ‘’

# I want to do all these three “with open(..)” at once.
with open(‘file01.txt’, ‘r’) as file01:
    for line in file01:
        something01 = do something in line
        file01_output += something01

with open(‘file02.txt’, ‘r’) as file01:
    for line in file01:
        something02 = do something in line
        file02_output += something02

with open(‘file03.txt’, ‘r’) as file01:
    for line in file01:
        something03 = do something in line
        file03_output += something03

def merge(a,b,c):
    a = file01_output
    b = file01_output
    c = file01_output

    # compile the list of dataframes you want to merge
    data_frames = [a, b, c]

    df_merged = reduce(lambda  left,right: pd.merge(left,right,
                       on=['common_column'], how='outer'), data_frames).fillna('.')

在你的问题中使用多重处理有很多种方法,所以我只提出一种方法。正如您所提到的,由于文件中数据的处理受到CPU的限制,您可以在单独的进程中运行该处理,并期望看到一些改进(如果有,改进的程度取决于问题、算法、内核等)。例如,整体结构可能看起来就像是有一个
,您可以
映射它
需要处理的所有
文件名的列表,并在该函数中进行计算

举一个具体的例子更容易。让我们假设我们有一个csv列表
'file01.csv',file02.csv',file03.csv'
,其中有一列
数字
,我们要计算该数字是否为素数(CPU限制)。例如,
file01.csv

NUMBER
1
2
3
...
其他文件看起来很相似,但编号不同,以避免重复工作。计算素数的代码可以如下所示:

import pandas as pd
from multiprocessing import Pool
from sympy import isprime

def compute(filename):
    # IO (probably not faster)
    my_data_df = pd.read_csv(filename)

    # do some computing (CPU)
    my_data_df['IS_PRIME'] = my_data_df.NUMBER.map(isprime)

    return my_data_df

if __name__ == '__main__':
    filenames = ['file01.csv', 'file02.csv', 'file03.csv']

    # construct the pool and map to the workers
    with Pool(2) as pool:
        results = pool.map(compute, filenames)
    print(pd.concat(results))

我使用了
sympy
软件包来实现一种方便的
isprime
方法,我确信我的数据结构是完全不同的,但希望该示例也能说明一种可以使用的结构。在
(或
进程列表
es)中执行CPU限制的计算,然后合并/减少/连接结果的计划是解决该问题的合理方法。

在普通python中打开文件并以数据帧列表结束的函数中似乎发生了很多事情。你确定你的瓶颈可以通过多处理文件读取来解决吗?你能用更清楚的话来解释吗?我读完3个文件不是一个瓶颈吗。我在想是否有一种方法可以同时读取所有三个文件;如果不加载内存中的所有3个文件,则并行处理。我想举一个不太好的例子,你说的是数据处理。那是不是
按行做些什么
部分?如果这真的是CPU受限的工作,那么多处理就是一条路。是的,这是CPU受限的工作。
并发。未来是你想要的。保罗,你提供了一个非常全面的解释,与我到目前为止读到的相比。非常感谢。我希望我能实现我现在所做的。我不得不重写我的问题,因为我在我的另一个问题中没有收到响应(虽然它不是重复的)。几周来,我一直在努力理解python中的这种并行化,但这是关于我的数据。你是否有时间把你的观点放在这个问题上。