系统相关
首页 > 系统相关> > python – 列表中的多进程多个文件

python – 列表中的多进程多个文件

作者:互联网

我试图读取一个列表,其中包含同步存储在列表中的N个.csv文件.

现在我做以下事情:

导入多进程

>空列表
>使用.csv的listdir附加列表
> def A() – 偶数文件(list [:: 2])
> def B() – 奇数文件(列表[1 :: 2]
>流程1 def A()
>过程2 def B()

def read_all_lead_files(folder):

    for files in glob.glob(folder+"*.csv"):
        file_list.append(files)
        def read_even():
           file_list[::2]    
        def read_odd():
           file_list[1::2]  

     p1 = Process(target=read_even)
     p1.start()
     p2 = Process(target=read_odd)
     p2.start()

有没有更快的方法将列表的分区拆分为Process函数?

解决方法:

我猜这里是根据你的要求,因为最初的问题还不清楚.由于os.listdir不保证排序,我假设你的“两个”函数实际上是相同的,你只需要同时对多个文件执行相同的过程.

根据我的经验,最简单的方法是启动池,为每个文件启动一个进程,然后等待.例如

import multiprocessing

def process(file):
    pass # do stuff to a file

p = multiprocessing.Pool()
for f in glob.glob(folder+"*.csv"):
    # launch a process for each file (ish).
    # The result will be approximately one process per CPU core available.
    p.apply_async(process, [f]) 

p.close()
p.join() # Wait for all child processes to close.

标签:python,multithreading,list,csv,multiprocess
来源: https://codeday.me/bug/20190830/1770663.html