gpt4 book ai didi

python - 正确使用 os.wait()?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:18:10 25 4
gpt4 key购买 nike

我正在尝试通过自动化我工作场所中使用的一系列脚本来解决问题。我是初学者,所以很可能是一个简单的问题(希望如此),我深表歉意,我已经阅读了相关文献,但对我来说并没有太大意义。

基本上我有一个运行 python 脚本的 bash 脚本和一个需要按顺序运行的 R 脚本,目前正在运行 R 脚本在 python 完成之前开始的代码,我在这里被告知我不能使用shell wait 函数,因为我的 python 脚本启动子进程,而 shell wait 不能用于等待孙子进程。

那很好,所以提供的解决方案是让 python 和 R 脚本在它们自己的子进程上等待,以便当它们退出时,bash 脚本可以正常按顺序运行。不幸的是,我无法在我的 python 脚本中找出正确的命名法。

这是我所拥有的:

cmd = "python %s/create_keyfile.py %s %s %s %s" %(input, input, input, 
input, input)
print cmd
os.system(cmd)

cmd = "python %s/uneak_name_plus_barcode_v2.py %s %s %s %s" %(input,
input, input, input, input)
print cmd
os.system(cmd)

cmd = "python %s/run_production_mode.py %s %s %s %s %s" %(input, input,
input, input, input, input)
print cmd
os.system(cmd)

“输入”是我代码中的实际输入,我可能无法准确分享我们正在做的事情:)

所以基本上我是想找出让整个脚本在退出前等待这三个脚本的最佳方法。

最佳答案

使用 subprocess.check_call() 而不是 os.system()

subprocess.check_call() 将阻止您的主要 Python 脚本的执行,直到该函数返回一个值。

Documentation for check_call() here

应始终使用子流程模块代替 os.system() 进行子流程管理和执行。

关于python - 正确使用 os.wait()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47897402/

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