用于创建多个.json文件的Pythoncurrent.futures ProcessPoolExecutor

问题描述 投票:0回答:1

我在通过函数创建.json文件并通过Python中的parallel.futures模块对其进行多处理时遇到问题。

我的代码如下:

import json, concurrent.futures

iterable_list = [string1, string2, string3, string4, ...]

def lovely_function(arg):
    with open(string1+'.json', 'w') as write_file:
        print('opening',string1,'.json file.')            

        ...loop through stuff and create a dictionary...

        print('closing',string1,'.json file.')
        json.dump(dictionary,write_file)

with concurrent.futures.ProcessPoolExecutor() as executor:
    executor.map(lovely_function, iterable_list)

[当我在控制台(Ubuntu 16.something)中运行脚本时,它将为iterable_list中的每个项目打印“ opening .json”部分,但只会打印一个“ closes .json”。但是,所有.json文件都已创建并转储到我的目标文件夹中,但是其中只有一个(带有结束语句的文件)包含数据。有时我根本没有“关闭.json”。

我已经仔细检查了我的函数,如果您手动输入它(例如lovely_function(string1)]),它就可以正常工作]

我做错了什么?有任何提示或提示吗?感谢您的帮助!

我在通过函数创建.json文件并通过Python中的current.futures模块对其进行多处理时遇到问题。我的代码如下所示:import json,current.futures ...

python json list multiprocessing concurrent.futures
1个回答
0
投票

似乎Django ORM(这是我在函数中进行数据改组的一部分)在任何情况下都无法与多处理一起很好地工作。如果您查看其他帖子,则总是与共享/打开连接有关。我会再看一下。

© www.soinside.com 2019 - 2024. All rights reserved.