gpt4 book ai didi

python - SCons 中 chdir=1 和 num_jobs>1 的组合

转载 作者:行者123 更新时间:2023-11-28 22:43:56 29 4
gpt4 key购买 nike

我有一个相当冗长的测试任务,它由 SCons 自动执行并且可以并行化。但是,它目前依赖于使用 chdir=1,目前要删除它并非易事。现在,只要我分别使用 -j2 SetOption('num_jobs', 2) 作业就会失败,下面的最小(非)工作示例说明了原因:chdir不是基于每个线程应用,而是一次影响所有作业。

这种行为是故意的吗?有什么办法可以防止这种情况发生吗?

# File SConstruct
import os, time

def my_build_fun(target, source, env):
for i in range(1, 5):
out = "my_build_fun: %d %s %s" % (i, str(source[0]), os.getcwd())
print out
time.sleep(0.5)
return None

bld = Builder(action = my_build_fun,
suffix = '.output',
src_suffix = '.input',
chdir=1)
env = Environment(BUILDERS = {'Foo' : bld})

Alias('do_a', env.Foo('folder_a/do_a'))
Alias('do_b', env.Foo('folder_b/do_b'))

Default(['do_a', 'do_b'])

运行于

+--- SConstruct
+-+- folder_a
| \--- do_a.input
\-+- folder_b
\--- do_b.input

scons -j1 的结果:符合预期

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
os.chdir('folder_a')
my_build_fun(["folder_a\do_a.output"], ["folder_a\do_a.input"])
my_build_fun: 1 folder_a\do_a.input H:\Playground\folder_a
my_build_fun: 2 folder_a\do_a.input H:\Playground\folder_a
my_build_fun: 3 folder_a\do_a.input H:\Playground\folder_a
my_build_fun: 4 folder_a\do_a.input H:\Playground\folder_a
os.chdir('H:\\Playground')
os.chdir('folder_b')
my_build_fun(["folder_b\do_b.output"], ["folder_b\do_b.input"])
my_build_fun: 1 folder_b\do_b.input H:\Playground\folder_b
my_build_fun: 2 folder_b\do_b.input H:\Playground\folder_b
my_build_fun: 3 folder_b\do_b.input H:\Playground\folder_b
my_build_fun: 4 folder_b\do_b.input H:\Playground\folder_b
os.chdir('H:\\Playground')
scons: done building targets.

scons -j2 的结果:尝试将 chdir 放入 .\folder_a\folder_b,但实际上并不存在。

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
os.chdir('folder_a')
my_build_fun(["folder_a\do_a.output"], ["folder_a\do_a.input"])
my_build_fun: 1 folder_a\do_a.input H:\Playground\folder_a
os.chdir('folder_b')
my_build_fun(["folder_b\do_b.output"], ["folder_b\do_b.input"])
scons: *** [folder_b\do_b.output] folder_b: The system cannot find the file specified
my_build_fun: 2 folder_a\do_a.input H:\Playground\folder_a
my_build_fun: 3 folder_a\do_a.input H:\Playground\folder_a
my_build_fun: 4 folder_a\do_a.input H:\Playground\folder_a
my_build_fun
os.chdir('H:\\Playground')
scons: building terminated because of errors.

最佳答案

来自 SCons 手册页

http://www.scons.org/doc/2.3.2/HTML/scons-man.html

WARNING: Python only keeps one current directory location for all of the threads. This means that use of the chdir argument will not work with the SCons -j option, because individual worker threads spawned by SCons interfere with each other when they start changing directory.

因此,如果您的脚本无法从构建的基本目录运行。解决该问题的(当前)最简单方法是创建一个脚本,该脚本将 cd 然后运行您的命令。或者甚至把它放在执行的shell命令中

env.Command(b,a,"cd ${SOURCE.dir}; do whatever -o ${TARGET.file} -i ${SOURCE.file}").

关于python - SCons 中 chdir=1 和 num_jobs>1 的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099640/

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