【问题标题】:Python concurrent.futures ProcessPoolExecutor for creating multiple .json-filesPython concurrent.futures ProcessPoolExecutor 用于创建多个 .json 文件
【发布时间】:2020-02-04 20:20:40
【问题描述】:

我在通过函数创建 .json 文件以及通过 Python 中的 concurrent.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 中的每个项目打印“打开的 .json”部分,但它只会打印一个“关闭的 .json”。但是,所有 .json 文件都被创建并转储到我的目标文件夹中,但其中只有一个(带有结束语句的那个​​)包含数据。有时我根本没有“关闭 .json”。

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

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

【问题讨论】:

  • 我可能需要补充一点,我在函数的主要部分使用 Django 查询。我不确定这是否会使事情变得更复杂。

标签: python json list multiprocessing concurrent.futures


【解决方案1】:

似乎 Django ORM(它是我在函数内进行数据混洗的一部分)在任何情况下都无法与多处理一起很好地工作。如果您查看不同的帖子,则始终是关于共享/打开连接的。我再看看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多