社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

RPalmer

RPalmer 最近创建的主题
RPalmer 最近回复了
6 年前
回复了 RPalmer 创建的主题 » 在python3中使用多处理进行文件读取

将传递到pool.map的列表中的元素不会自动解包。在“文件读取”函数中,通常只能有一个参数。

当然,这个参数可以是一个元组,所以您自己解包它是没有问题的:

def file_reading(args):
    file, num_of_sample, segsites, positions, snp_matrix = args
    with open(file,buffering=2000009999) as f:
        ###I read file here. I am not putting that code here.
        try:
            assert len(snp_matrix) == len(positions)
            return positions,snp_matrix ## return statement
        except:
             print('length of snp matrix and length of position vector not the same.')
            sys.exit(1)

if __name__ == "__main__":    
    segsites = []
    positions = []
    snp_matrix = []

    path_to_directory = '/dataset/example/'
    extension = '*.msOut'

    num_of_samples = 162
    filename = glob.glob(path_to_directory+extension)

    number_of_workers = 10

    x,y,z = [],[],[]


    array_of_number_tuple = [(filename[file], num_of_samples, segsites,positions,snp_matrix) for file in range(len(filename))]
    with multiprocessing.Pool(number_of_workers) as p:
        pos,snp = p.map(file_reading,array_of_number_tuple)
        x.extend(pos)
        y.extend(snp)