“进程已分叉,您无法安全地使用此 CoreFoundation 功能。您必须执行()。”是啥意思?错误是啥意思?

Posted

技术标签:

【中文标题】“进程已分叉,您无法安全地使用此 CoreFoundation 功能。您必须执行()。”是啥意思?错误是啥意思?【英文标题】:What does "The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec()." error mean?“进程已分叉,您无法安全地使用此 CoreFoundation 功能。您必须执行()。”是什么意思?错误是什么意思? 【发布时间】:2021-01-18 07:23:43 【问题描述】:

这个错误是什么意思?似乎当我在进程下使用一些 C++ 包装代码时会发生这种情况(当我在普通 for 循环中使用它时没有错误)。

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

示例代码:

def split_to_chunks(items, k):
    n = len(items) // k
    chunks = []
    for i in range(0, len(items), n):
        chunks.append(items[i:i + n])
    return chunks

def process_chunk(process_id, img_filepaths, output_dir):
    print('Enter process_id:', process_id)
    for img_filepath in img_filepaths:
        try:
           //Code section
        except Exception as e:
            print(f'Error: process_id: process_id : img_filepath', e)
    print('End process_id:', process_id)

img_filepaths = get_img_filepaths(images_dir)

#n_processes = multiprocessing.cpu_count() // 2
n_processes = 1
img_filepaths_chunks = split_to_chunks(img_filepaths, n_processes)

processes = []
for i in range(n_processes):
    process = multiprocessing.Process(
        target=process_chunk,
        args=(i, img_filepaths_chunks[i], output_dir)
    )
    processes.append(process)
for process in processes:
    process.start()
for i, process in enumerate(processes):
    process.join()
    if process.exitcode != 0:
        print(f'Error: process_id: i failed.')
        #sys.exit(1)

【问题讨论】:

【参考方案1】:

似乎使用这里建议的“spawn”https://turtlemonvh.github.io/python-multiprocessing-and-corefoundation-libraries.html 正在解决问题。

import multiprocessing as mp
mp.set_start_method('spawn')

【讨论】:

以上是关于“进程已分叉,您无法安全地使用此 CoreFoundation 功能。您必须执行()。”是啥意思?错误是啥意思?的主要内容,如果未能解决你的问题,请参考以下文章