gpt4 book ai didi

python - 使用 Python 多处理在 worker 之间共享一个变量

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:58 30 4
gpt4 key购买 nike

<分区>

如何在 Python 中读取和更新多个 worker 之间共享的变量?

例如,我在 Python 中使用多个进程扫描文件列表,并想检查父目录是否已被扫描。

def readFile(filename):
""" Add the parent folder to the database and process the file
"""

path_parts = os.path.split(filename)
dirname = os.path.basename(path_parts[0])
if dirname not in shared_variable:
# Insert into the database


#Other file functions


def main():
""" Walk through files and pass each file to readFile()
"""
queue = multiprocessing.Queue()
pool = multiprocessing.Pool(None, init, [queue])

for dirpath, dirnames, filenames in os.walk(PATH):

full_path_fnames = map(lambda fn: os.path.join(dirpath, fn),
filenames)
pool.map(readFile, full_path_fnames)

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com