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

标签 python linux shell wait

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

基本上我有一个运行 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/

相关文章:

php - Python无法连接MySQL

python - 验证 ModelForm 的最佳方法

python - Requestscookiejar 为空,即使页面有它

linux - 通过中间机器挂载远程文件系统(sshfs)

linux - 文件所有权不会停留在 git 分支更改上

java - 如何在 Mac OS X 上的 bash 脚本中更改当前工作目录?

shell - 如果文件编码为 utf-16le,则获取垃圾字符

python - 如何将 twitter API v2 时间戳解析为 python 日期时间

c - 应该在每次 fclose 之后使用 fsync 吗?

子shell进程双向重定向到父进程