Python 为子进程创建超时
Python Create Timeout for subprocess
我正在编写 Python 脚本,它将刷新 Git 存储库,然后构建它。
该脚本将每天 运行(例如 运行ning 在每日构建服务器上)。
问题是对我们服务器的 git 命令有时永远不会完成(好的,我会在 10 分钟或更长时间后终止进程)。
我想为子进程设置一个超时(并可能放入重试循环)。
如何为子进程设置超时。
到目前为止,这是我的代码:
#------------------------------------------------------------------------------
# Script to daily build Voyant 3
#------------------------------------------------------------------------------
import subprocess
import os
#------------------------------------------------------------------------------
# Main program starts here.
#------------------------------------------------------------------------------
repo_dir = "c:/sandboxes/git/voyant-3"
#------------------------------------------------------------------------------
# Change to repo (repository) folder.
#------------------------------------------------------------------------------
os.chdir(repo_dir);
#------------------------------------------------------------------------------
# Refresh the repo (sandbox)
#------------------------------------------------------------------------------
subprocess.run(["git", "fetch", "--all]"]);
备注:
- 使用 Python 27(首选)或 Python 36
- 使用Windows10(脚本会在PC上注册为服务)
- 关于使用 Py 的研究Git 并不令人印象深刻;大多数用户说使用 Python Subprocess.
- 来自 Python 文档:(https://docs.python.org/3/library/subprocess.html):
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)
while True:
try: subprocess.run(["git", "fetch", "--all"], timeout=600);
except subprocess.TimeoutExpired: continue
break
我正在编写 Python 脚本,它将刷新 Git 存储库,然后构建它。
该脚本将每天 运行(例如 运行ning 在每日构建服务器上)。
问题是对我们服务器的 git 命令有时永远不会完成(好的,我会在 10 分钟或更长时间后终止进程)。
我想为子进程设置一个超时(并可能放入重试循环)。
如何为子进程设置超时。
到目前为止,这是我的代码:
#------------------------------------------------------------------------------
# Script to daily build Voyant 3
#------------------------------------------------------------------------------
import subprocess
import os
#------------------------------------------------------------------------------
# Main program starts here.
#------------------------------------------------------------------------------
repo_dir = "c:/sandboxes/git/voyant-3"
#------------------------------------------------------------------------------
# Change to repo (repository) folder.
#------------------------------------------------------------------------------
os.chdir(repo_dir);
#------------------------------------------------------------------------------
# Refresh the repo (sandbox)
#------------------------------------------------------------------------------
subprocess.run(["git", "fetch", "--all]"]);
备注:
- 使用 Python 27(首选)或 Python 36
- 使用Windows10(脚本会在PC上注册为服务)
- 关于使用 Py 的研究Git 并不令人印象深刻;大多数用户说使用 Python Subprocess.
- 来自 Python 文档:(https://docs.python.org/3/library/subprocess.html):
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)
while True:
try: subprocess.run(["git", "fetch", "--all"], timeout=600);
except subprocess.TimeoutExpired: continue
break