Python多处理竞争条件

问题描述 投票:2回答:2

当我使用concurrent.futures从多个文本文件中读取时,我发现了一个奇怪的错误。

这是一个可重复的小例子:

import os
import concurrent.futures

def read_file(file):
    with open(os.path.join(data_dir, file),buffering=1000) as f:
        for row in f:
            try:
                print(row)
            except Exception as e:
                print(str(e))

if __name__ == '__main__':
    data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data'))
    files = ['file1', 'file2']
    with concurrent.futures.ProcessPoolExecutor() as executor:
        for file,_ in zip(files,executor.map(read_file,files)):
            pass    

file1file2data目录中的任意文本文件。

我收到以下错误(基本上一个进程尝试在分配之前读取data_dir变量):

concurrent.futures.process._RemoteTraceback:
"""
Traceback (most recent call last):
  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\concurrent\futures\process.py", line 175, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\concurrent\futures\process.py", line 153, in _process_chunk
    return [fn(*args) for args in chunk]
  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\concurrent\futures\process.py", line 153, in <listcomp>
    return [fn(*args) for args in chunk]
  File "C:\Users\my_username\Downloads\example.py", line 5, in read_file
    with open(os.path.join(data_dir, file),buffering=1000) as f:
NameError: name 'data_dir' is not defined
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "example.py", line 16, in <module>
    for file,_ in zip(files,executor.map(read_file,files)):
  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\concurrent\futures\_base.py", line 556, in result_iterator
    yield future.result()
  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\concurrent\futures\_base.py", line 405, in result
    return self.__get_result()
  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\concurrent\futures\_base.py", line 357, in __get_result
    raise self._exception
NameError: name 'data_dir' is not defined

如果我在data_dir块之前放置if __name__ == '__main__':赋值,我不会收到此错误并且代码按预期执行。

导致此错误的原因是什么?显然,在两种情况下都应该进行任何异步调用之前分配data_dir

python python-3.x multiprocessing race-condition
2个回答
2
投票

fork()在windows上不可用,所以python使用spawn启动新进程,这将启动一个新的python解释器进程,没有内存将被共享,但python将在新进程中try to recreate worker函数环境,这就是为什么模块级变量有效。见doc for more detail


3
投票

ProcessPoolExecutor产生一个新的Python进程,导入正确的模块并调用您提供的功能。由于data_dir仅在运行模块时定义,而不是在导入模块时定义,因此可能会出现错误。

提供data_dir文件描述符作为read_file的参数可能有效,因为我相信进程继承了父进程的文件描述符。但是你需要检查一下。

但是,如果要使用ThreadPoolExecutor,那么您的示例应该可以工作,因为生成的线程共享内存。

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